🤝 Collaboration¶
Collaboration involves working with remote repositories and other developers using Git.
✅ Remote Repositories¶
A remote is a version of your project hosted on the internet or network:
✅ Common Git Hosts¶
| Platform | URL |
|---|---|
| GitHub | https://github.com |
| GitLab | https://gitlab.com |
| Bitbucket | https://bitbucket.org |
✅ Adding Remotes¶
# Add a remote
git remote add origin https://github.com/username/repo.git
# Add a remote with a different name
git remote add upstream https://github.com/original-owner/repo.git
# View remote URL
git remote get-url origin
# Change remote URL
git remote set-url origin https://github.com/username/new-repo.git
✅ Listing Remotes¶
# Show all remotes
git remote
# Show remotes with URLs
git remote -v
# Show details for a specific remote
git remote show origin
✅ Cloning a Repository¶
# Clone a repository
git clone https://github.com/username/repo.git
# Clone into a specific directory
git clone https://github.com/username/repo.git my-directory
# Clone a specific branch
git clone -b develop https://github.com/username/repo.git
# Clone with SSH (requires SSH key)
git clone git@github.com:username/repo.git
Cloning creates:
- A local repository
- A remote named origin
- All branches as remote-tracking branches
✅ Fetching Changes¶
Download changes from remote without merging:
# Fetch all changes from origin
git fetch origin
# Fetch a specific branch
git fetch origin main
# Fetch all remotes
git fetch --all
# Fetch and prune deleted remote branches
git fetch --prune
Fetch updates your remote-tracking branches (origin/main, origin/feature) but doesn't change your local branches.
✅ Pulling Changes¶
Fetch and merge in one command:
# Pull from origin (default branch)
git pull
# Pull from a specific remote/branch
git pull origin main
# Pull with rebase instead of merge
git pull --rebase
# Pull without committing merge automatically
git pull --no-commit
✅ Pushing Changes¶
Upload your commits to a remote:
# Push current branch to remote
git push
# Push to a specific remote/branch
git push origin main
# Push all branches
git push --all
# Push tags
git push --tags
# Force push (use carefully!)
git push --force
# Force push with lease (safer)
git push --force-with-lease
✅ Setting Upstream Branch¶
Connect a local branch to track a remote branch:
# Push and set upstream
git push -u origin feature-login
# Set upstream without pushing
git branch --set-upstream-to=origin/feature-login feature-login
# Show tracking information
git branch -vv
After setting upstream, git push and git pull use the default remote/branch.
✅ Viewing Remote Branches¶
# List remote branches
git branch -r
# Show remote and local branches
git branch -a
# Checkout a remote branch
git checkout feature-login
# Creates local branch tracking origin/feature-login
# Or explicitly:
git checkout -b feature-login origin/feature-login
✅ Forking Workflow¶
For contributing to open source:
- Fork - Create your copy of the repository
- Clone - Clone your fork locally
- Create branch - Work on a feature branch
- Commit & push - Push to your fork
- Pull Request - Request to merge into original repo
# 1. Fork on GitHub (web UI)
# 2. Clone your fork
git clone https://github.com/YOUR-USERNAME/repo.git
cd repo
# 3. Add original as upstream
git remote add upstream https://github.com/ORIGINAL-OWNER/repo.git
# 4. Create feature branch
git checkout -b feature-new-stuff
# 5. Commit changes
git add .
git commit -m "Add new feature"
# 6. Push to your fork
git push -u origin feature-new-stuff
# 7. Create Pull Request on GitHub (web UI)
# 8. Update with upstream changes
git fetch upstream
git rebase upstream/main
git push origin feature-new-stuff
✅ Pull Requests / Merge Requests¶
Propose changes for review before merging:
Pull Request (PR) Workflow: 1. Push feature branch to remote 2. Create PR on GitHub/GitLab 3. Discuss changes with team 4. Make requested changes 5. Get approval 6. Merge into main 7. Delete feature branch
# Prepare PR
git checkout -b feature-xyz
# ... work ...
git push -u origin feature-xyz
# Then create PR on web UI
✅ Code Review Process¶
Developer PR Platform Reviewer
| | |
|-- push branch ----->| |
| |-- notify ---------> |
| |<-- comments -------- |
|<-- comments -------| |
|-- push updates --->| |
| |-- notify ---------> |
| |<-- approve -------- |
| |-- merge -----------> |
✅ Syncing with Fork¶
Keep your fork updated with the original repository:
# Add upstream remote (once)
git remote add upstream https://github.com/ORIGINAL-OWNER/repo.git
# Fetch latest from upstream
git fetch upstream
# Update your local main branch
git checkout main
git merge upstream/main
# Update your fork on GitHub
git push origin main
# Or update feature branch
git checkout feature-xyz
git rebase upstream/main
git push origin feature-xyz
✅ Collaborating on the Same Repo¶
For teams working on the same repository:
# 1. Always start from latest main
git checkout main
git pull origin main
# 2. Create feature branch
git checkout -b feature-xyz
# 3. Work and commit
# ... make changes ...
git add .
git commit -m "Add feature"
# 4. Keep branch updated
git fetch origin
git rebase origin/main
# 5. Push and create PR
git push -u origin feature-xyz
# 6. After merge, update and delete branch
git checkout main
git pull origin main
git branch -d feature-xyz
✅ Handling Merge Conflicts in PR¶
When your PR has conflicts with main:
# 1. Fetch latest main
git fetch origin
# 2. Rebase your branch
git checkout feature-xyz
git rebase origin/main
# 3. Resolve conflicts
# ... edit files ...
git add .
git rebase --continue
# 4. Force push (you rewrote history)
git push --force-with-lease origin feature-xyz
✅ SSH vs HTTPS¶
| Protocol | HTTPS | SSH |
|---|---|---|
| Auth | Username/password | SSH key |
| Setup | None | Generate SSH key |
| Security | Good | Better |
| Use | Simple scripts | Frequent pushes |
# HTTPS clone
git clone https://github.com/username/repo.git
# SSH clone
git clone git@github.com:username/repo.git
✅ Personal Access Tokens¶
For GitHub with HTTPS, use Personal Access Tokens (PAT):
- Go to GitHub Settings → Developer Settings → Personal Access Tokens
- Generate a new token
- Use token as password when pushing
✅ Git LFS (Large File Storage)¶
For large files (videos, datasets, etc.):
# Install Git LFS
git lfs install
# Track large files
git lfs track "*.psd"
git lfs track "*.mp4"
# Commit .gitattributes
git add .gitattributes
git commit -m "Track large files"
# Now use Git normally
git add largefile.psd
git commit -m "Add design file"
git push
✅ Best Practices¶
- Pull before push - Always sync before pushing
- Write good PR descriptions - Explain what and why
- Keep PRs small - Easier to review
- Respond to feedback - Address reviewer comments
- Delete merged branches - Keep repository clean
- Use branches - Never work directly on main
- Sync frequently - Reduce merge conflicts
✅ Common Collaboration Commands¶
# Daily workflow
git checkout main
git pull origin main
git checkout -b feature
# ... work ...
git push -u origin feature
# Syncing
git fetch --all
git status
# Review
git log --graph --oneline origin/main...feature
git diff origin/main
🔍 Key Takeaways¶
- Remote repositories enable collaboration via push/pull
git clonecopies a remote repository locallygit fetchdownloads without merginggit pull= fetch + mergegit pushuploads your commits- PRs/MRs are how teams review and merge code
- Forking is for contributing to open source
- Always sync before working