Learning Objectives
By the end of this lesson you will be able to:
- Explain the difference between
git fetchandgit pull - Describe what a remote-tracking branch is and why it can be stale
- Inspect incoming changes before deciding to take them
- Prune branches that no longer exist on the server
Fetch Is the Safe One
git fetch origin
From github.com:acme/payments
* [new branch] feature/PAY-4102-new-auth-flow -> origin/feature/PAY-4102-new-auth-flow
8f3c2a1..d4e5f6a main -> origin/main
This command downloads everything new from the server and changes nothing you are working on. Not your branch, not your working directory, not your uncommitted changes, not your staging area.
All it updates is your picture of the server — the remote-tracking branches. It is the single safest command in Git, and there is never a reason to hesitate before running it.
Fetch vs. Pull
git pull = git fetch + git merge origin/<current-branch>
git pull fetches and immediately merges into your current branch. It is convenient, and it is also how people end up with an unexpected merge, or a conflict, in the middle of doing something else.
git fetch separates the two halves so you can look before you leap:
git fetch origin # 1. get the data
git log --oneline HEAD..origin/main # 2. what would arrive?
git diff HEAD..origin/main --stat # 3. which files?
git merge origin/main # 4. take it, deliberately
Four commands instead of one. On a busy team that habit prevents a real category of surprise, especially when you are mid-investigation and cannot afford your working tree to change under you.
Remote-Tracking Branches
After a fetch, you have references like origin/main and origin/feature/PAY-4102-new-auth-flow.
These are read-only local snapshots of where the server's branches were at the moment you last fetched. Three consequences that matter:
They are not live. origin/main does not update by itself. If you fetched two hours ago, origin/main is two hours old, no matter what the server has done since. Anyone confused about "why doesn't Git see the new branch?" has almost always just not fetched.
They are not yours to commit to. You cannot commit onto origin/main. Checking it out puts you in detached HEAD — which is fine for looking, as Module 1 covered.
They are what .. comparisons should use. git log HEAD..origin/main means "what does the server have that I don't?", which is exactly the question worth asking before a merge.
Seeing What Exists
git branch -r
origin/HEAD -> origin/main
origin/main
origin/develop
origin/feature/PAY-4102-new-auth-flow
origin/hotfix/PAY-4417-double-charge
Remote branches only. Add -a to see local ones as well.
git remote show origin
More detail: which branches are tracked, which are stale, and which local branches are configured to push where. Note that this one contacts the server.
Pruning Deleted Branches
When a PR merges, the platform usually deletes the branch on the server. Your local origin/... reference does not disappear on its own, so your branch list slowly fills with branches that no longer exist.
git fetch --prune
- [deleted] (none) -> origin/feature/PAY-4001-old-flow
Removes remote-tracking references whose branches are gone from the server. It does not touch your local branches — only your picture of the remote.
Make it automatic:
git config --global fetch.prune true
Do this once. A branch list that reflects reality is worth having.
Fetching Just One Branch
On a very large repository, fetching everything can be slow:
git fetch origin feature/PAY-4102-new-auth-flow
Fetches only that branch. Useful when you have been sent a PR branch name and want just that.
A Tester's Routine
Every morning, and before every test session:
git fetch --prune # refresh the picture, tidy the dead
git log --oneline HEAD..origin/main # what has the team done?
git branch -r | grep PAY-4102 # is the branch I need there?
Three commands, ten seconds, and nothing in your working directory has changed.
Pro Tip: When a developer says "I pushed it" and you can't find the branch — fetch first, then look. It is the answer roughly nine times out of ten, and it is faster than asking.
Key Takeaways
git fetchdownloads from the server and changes nothing you are working ongit pullisfetchplus an immediate merge; splitting them lets you look before taking- Remote-tracking branches like
origin/mainare snapshots from your last fetch, not live git log HEAD..origin/mainshows exactly what a merge would bring ingit fetch --pruneremoves references to branches deleted on the server; enable it globally- If you cannot see a branch a colleague pushed, fetch before you ask