The Hook (The "Byte-Sized" Intro)
git fetch is the polite way to check what's new. It downloads all the latest commits, branches, and tags from the remote — but it doesn't touch your working files. Nothing changes in your code. Nothing merges. You're just getting the latest information so you can review it before deciding what to do. It's the "peek before you leap" command.
📖 What is git fetch?
git fetch downloads commits, branches, and tags from a remote repository into your local repo's remote-tracking branches (e.g., origin/main). It updates your knowledge of the remote's state without modifying your working tree, staging area, or local branches.
Conceptual Clarity
git fetchupdates remote-tracking branches (origin/main,origin/develop, etc.)- Your local branches are NOT affected —
mainstays exactly where it was - Your working tree is NOT modified — no files change
- After fetching, you can inspect changes before merging them in
git pull=git fetch+git merge(fetch is the safe half)
What gets updated:
| Component | Before Fetch | After Fetch |
|---|---|---|
origin/main | Points to old commit | Points to latest remote commit |
Local main | Unchanged | Unchanged |
| Working tree | Unchanged | Unchanged |
| New remote branches | Not visible | Now visible as origin/<name> |
Real-Life Analogy
git fetch is like checking your mailbox. You bring all the new letters inside, but you don't open or act on them yet. You can look at the envelopes, decide which ones matter, and read them at your own pace. git pull opens and processes all the mail immediately.
Visual Architecture
Why It Matters
- Safety: See what's new without risking merge conflicts in your active work.
- Review first: Inspect remote changes with
git logorgit diffbefore merging. - Awareness: Know if your branch is behind, ahead, or diverged from the remote.
- Selective merging: Fetch once, then cherry-pick or merge only what you need.
Code
# ─── Fetch from the default remote (origin) ───
git fetch
# Downloads new commits, branches, and tags
# ─── Fetch from a specific remote ───
git fetch upstream
# ─── Fetch all remotes at once ───
git fetch --all
# ─── Fetch and prune deleted remote branches ───
git fetch --prune
# Removes local references to branches deleted on the remote
# ─── After fetching: compare local vs remote ───
git log main..origin/main --oneline
# Shows commits on remote that you don't have locally
git diff main origin/main
# Shows file-level differences
# ─── After fetching: merge when ready ───
git merge origin/main
# Now your local main is up-to-date
# ─── Check ahead/behind status ───
git status
# Output: Your branch is behind 'origin/main' by 3 commitsgit fetch vs git pull
| Aspect | git fetch | git pull |
|---|---|---|
| Downloads remote changes | ✅ | ✅ |
| Updates remote-tracking branches | ✅ | ✅ |
| Modifies local branch | ❌ | ✅ (merges automatically) |
| Modifies working tree | ❌ | ✅ |
| Can cause merge conflicts | ❌ | ✅ |
| Lets you review first | ✅ | ❌ (merges immediately) |
Key Takeaways
git fetchdownloads remote updates without changing your local branches or files.- It's the safe first step — always fetch before deciding to merge.
- Use
git log main..origin/mainafter fetching to see what's new. git pull=git fetch+git merge. Fetch is the non-destructive half.
Interview Prep
-
Q: What is the difference between
git fetchandgit pull? A:git fetchdownloads remote changes into remote-tracking branches but doesn't modify local branches or the working tree.git pulldoes a fetch AND automatically merges the remote changes into your current local branch. -
Q: Why would you use
git fetchinstead ofgit pull? A:git fetchlets you review remote changes before integrating them. You can inspect new commits withgit log, check diffs, and decide whether to merge, rebase, or cherry-pick — without risking unexpected merge conflicts in your working tree. -
Q: What are remote-tracking branches? A: Remote-tracking branches (e.g.,
origin/main) are local references that mirror the state of branches on the remote. They're updated bygit fetchand represent your last known snapshot of the remote's branches.