Lesson 2: The Sync
In a team, everyone pushes changes. Your local repository quickly falls out of sync with the remote. Knowing how to sync properly is the difference between smooth collaboration and merge conflict nightmares.
Fetch vs. Pull
This is one of the most confused concepts in Git:
| Command | Downloads? | Merges? | Safe? |
|---------|-----------|---------|-------|
| git fetch | ✅ Yes | ❌ No | ✅ Very safe |
| git pull | ✅ Yes | ✅ Yes | ⚠️ Can cause conflicts |
git fetch — The Safe Download
fetch downloads new commits from the remote but doesn't touch your working files. It's like checking your mailbox without opening the letters.
git fetch origin
After fetching, you can:
git log main..origin/main --oneline # See what's new on remote
git diff main origin/main # See the actual changes
Then decide to merge when you're ready:
git merge origin/main
git pull — Fetch + Merge
pull is a shortcut that fetches AND merges in one step:
git pull origin main
This is equivalent to:
git fetch origin
git merge origin/main
Pull with Rebase
For a cleaner history, use --rebase instead of merge:
git pull --rebase origin main
This replays your local commits on top of the remote changes, avoiding unnecessary merge commits.
Tracking Branches
When you clone a repo or push with -u, Git creates tracking relationships:
git branch -vv
* main a1b2c3d [origin/main] Latest commit message
feature e4f5g6h [origin/feature: ahead 2] WIP: login
ahead 2— You have 2 local commits not yet pushed.behind 3— The remote has 3 commits you haven't pulled.
The Sync Workflow
# Morning routine:
git fetch origin # 1. Check what's new
git log main..origin/main # 2. Review new changes
git pull --rebase origin main # 3. Sync up cleanly
# After your work:
git push origin main # 4. Share your work
Mission Objective
Master the art of staying in sync:
- Check mail: Run
git fetch originto download remote changes safely. - Review: Run
git log main..origin/main --onelineto see what's new. - Sync cleanly: Run
git pull --rebase origin mainfor a clean update.