Skip to main content

Git Collaboration

Git Distributed Model Git’s distributed nature makes team collaboration seamless. Learn to work with remote repositories, pull requests, and code review workflows.

Remote Repositories

A remote is a version of your repository hosted elsewhere (GitHub, GitLab, Bitbucket).

Viewing Remotes

# List remotes
git remote

# List with URLs
git remote -v

# Show remote details
git remote show origin

Adding Remotes

# Add a remote
git remote add origin https://github.com/username/repo.git

# Add multiple remotes
git remote add upstream https://github.com/original/repo.git

# Change remote URL
git remote set-url origin [email protected]:username/repo.git

Cloning Repositories

# Clone via HTTPS
git clone https://github.com/username/repo.git

# Clone via SSH
git clone [email protected]:username/repo.git

# Clone specific branch
git clone -b develop https://github.com/username/repo.git

# Shallow clone (faster, less history)
git clone --depth 1 https://github.com/username/repo.git

Pushing Changes

# Push to remote
git push origin main

# Push new branch
git push -u origin feature/new-feature
# -u sets upstream tracking

# Push all branches
git push origin --all

# Push tags
git push origin --tags

# Force push (DANGEROUS!)
git push --force origin main
Never force push to shared branches! It rewrites history and can cause problems for teammates.Safe alternative:
git push --force-with-lease origin main

Pulling Changes

# Fetch + merge
git pull origin main

# Fetch only (no merge)
git fetch origin

# Pull with rebase
git pull --rebase origin main

# Pull all branches
git fetch --all

Pull vs Fetch

CommandWhat it does
git fetchDownloads changes but doesn’t merge
git pullDownloads and merges (fetch + merge)
# Recommended workflow
git fetch origin
git log origin/main  # Review changes
git merge origin/main  # Merge when ready

Working with Forks

Forking Workflow

1

Fork on GitHub

Click “Fork” button on the repository
2

Clone your fork

git clone https://github.com/YOUR-USERNAME/repo.git
cd repo
3

Add upstream remote

git remote add upstream https://github.com/ORIGINAL-OWNER/repo.git
git remote -v
4

Keep fork updated

git fetch upstream
git checkout main
git merge upstream/main
git push origin main

Pull Requests (PRs)

Pull requests are how you propose changes to a repository.

Creating a Pull Request

# 1. Create feature branch
git checkout -b feature/add-search

# 2. Make changes and commit
git add .
git commit -m "feat: add search functionality"

# 3. Push to your fork
git push origin feature/add-search

# 4. Go to GitHub and click "Create Pull Request"

PR Best Practices

Small PRs

Easier to review, faster to merge

Clear Description

Explain what and why, not just how

Link Issues

Reference related issues: “Fixes #123”

Update Branch

Keep PR branch updated with main

PR Template Example

## Description
Brief description of changes

## Type of Change
- [ ] Bug fix
- [ ] New feature
- [ ] Breaking change
- [ ] Documentation update

## Testing
How to test these changes

## Screenshots
If applicable

## Checklist
- [ ] Code follows style guidelines
- [ ] Self-review completed
- [ ] Comments added for complex code
- [ ] Documentation updated
- [ ] Tests added/updated

Code Review

As a Reviewer

# Fetch PR branch
git fetch origin pull/123/head:pr-123
git checkout pr-123

# Test locally
npm install
npm test

# Leave comments on GitHub
Good Review Comments:
  • ✅ “Consider using a Map here for O(1) lookup”
  • ✅ “This could cause a race condition if…”
  • ✅ “Great solution! Minor: variable name could be more descriptive”
Bad Review Comments:
  • ❌ “This is wrong”
  • ❌ “Why did you do it this way?”
  • ❌ “I would have done it differently”

As an Author

# Address review comments
git add .
git commit -m "refactor: apply review suggestions"
git push origin feature/add-search

# Squash commits before merge (optional)
git rebase -i main

Collaboration Workflows

Centralized Workflow

# Everyone works on main
git clone https://github.com/team/repo.git
git pull origin main
# ... make changes ...
git add .
git commit -m "feat: add feature"
git pull origin main  # Get latest
git push origin main
Best for: Small teams, simple projects

Feature Branch Workflow

# Each feature gets its own branch
git checkout -b feature/user-profile
# ... work ...
git push origin feature/user-profile
# Create PR
# After review and merge
git checkout main
git pull origin main
git branch -d feature/user-profile
Best for: Most teams, medium to large projects

Forking Workflow

# Fork repo on GitHub
git clone https://github.com/YOUR-USERNAME/repo.git
git remote add upstream https://github.com/ORIGINAL/repo.git

# Create feature
git checkout -b feature/improvement
# ... work ...
git push origin feature/improvement
# Create PR from your fork to original repo
Best for: Open source projects, external contributors

Handling Common Scenarios

Syncing Fork with Upstream

# Fetch upstream changes
git fetch upstream

# Merge into your main
git checkout main
git merge upstream/main

# Push to your fork
git push origin main

Updating PR Branch

# Method 1: Merge
git checkout feature/my-pr
git merge main
git push origin feature/my-pr

# Method 2: Rebase (cleaner history)
git checkout feature/my-pr
git rebase main
git push --force-with-lease origin feature/my-pr

Resolving PR Conflicts

# Update your branch
git checkout feature/my-pr
git fetch origin
git merge origin/main

# Resolve conflicts
# ... edit files ...
git add .
git commit

# Push resolution
git push origin feature/my-pr

SSH vs HTTPS

HTTPS

git clone https://github.com/username/repo.git
Pros: Easy setup, works everywhere
Cons: Need to enter credentials (or use credential helper)

SSH

git clone [email protected]:username/repo.git
Pros: No password needed, more secure
Cons: Requires SSH key setup

Setting Up SSH

# Generate SSH key
ssh-keygen -t ed25519 -C "[email protected]"

# Start SSH agent
eval "$(ssh-agent -s)"

# Add key
ssh-add ~/.ssh/id_ed25519

# Copy public key
cat ~/.ssh/id_ed25519.pub
# Add to GitHub: Settings → SSH Keys → New SSH Key

Tags and Releases

Creating Tags

# Lightweight tag
git tag v1.0.0

# Annotated tag (recommended)
git tag -a v1.0.0 -m "Release version 1.0.0"

# Tag specific commit
git tag -a v0.9.0 abc123

# List tags
git tag

# Push tags
git push origin v1.0.0
git push origin --tags  # All tags

Semantic Versioning

v1.2.3
│ │ └─ Patch: Bug fixes
│ └─── Minor: New features (backward compatible)
└───── Major: Breaking changes

Practical Examples

Example 1: Contributing to Open Source

# 1. Fork repo on GitHub

# 2. Clone your fork
git clone https://github.com/YOUR-USERNAME/awesome-project.git
cd awesome-project

# 3. Add upstream
git remote add upstream https://github.com/ORIGINAL/awesome-project.git

# 4. Create branch
git checkout -b fix/typo-in-readme

# 5. Make changes
echo "Fixed typo" >> README.md
git add README.md
git commit -m "docs: fix typo in README"

# 6. Push to your fork
git push origin fix/typo-in-readme

# 7. Create PR on GitHub

# 8. Keep fork updated
git fetch upstream
git checkout main
git merge upstream/main
git push origin main

Example 2: Team Feature Development

# 1. Get latest code
git checkout main
git pull origin main

# 2. Create feature branch
git checkout -b feature/payment-integration

# 3. Work and commit
git add .
git commit -m "feat: add Stripe payment integration"

# 4. Push and create PR
git push -u origin feature/payment-integration

# 5. Address review comments
git add .
git commit -m "refactor: apply code review suggestions"
git push origin feature/payment-integration

# 6. After merge, clean up
git checkout main
git pull origin main
git branch -d feature/payment-integration

Troubleshooting

”Your branch is behind ‘origin/main’"

git pull origin main

"Your branch has diverged"

# Option 1: Merge
git pull origin main

# Option 2: Rebase
git pull --rebase origin main

"Permission denied (publickey)"

# Check SSH key
ssh -T [email protected]

# If fails, add SSH key to GitHub
cat ~/.ssh/id_ed25519.pub

"Failed to push some refs”

# Someone pushed before you
git pull origin main
git push origin main

Best Practices

Always get latest changes before pushing
git pull origin main
git push origin main
  • One feature/fix per PR
  • Easier to review
  • Faster to merge
  • What changed
  • Why it changed
  • How to test
  • Screenshots if UI changes
  • Be constructive
  • Ask questions
  • Suggest improvements
  • Approve when ready

Key Takeaways

  • Remotes connect local and hosted repositories
  • Pull requests enable code review
  • Forks allow external contributions
  • SSH keys simplify authentication
  • Good collaboration requires communication

Next: Git Advanced Topics →