git fetch
git fetch downloads new commits, branches, and tags from a remote but does not touch your working tree or your local branches. It updates the remote-tracking refs (origin/main, origin/feature-x) so you can compare, inspect, or merge later. Knowing fetch vs. pull is the single fastest way to look like you know git.
Fetch, inspect, then choose how to integrate
EXAMPLE
# 1) Plain fetch from the default remote (origin), all branches and tags git fetch # 2) Fetch a specific remote git fetch upstream # 3) Prune deleted remote branches from your local view git fetch --prune git fetch -p origin # After fetching, the new history lives under remotes/origin/* git log --oneline --decorate --graph origin/main ^main git log HEAD..origin/main # what's on origin/main but not on me git log origin/main..HEAD # what's on me but not on origin/main # 4) Diff your branch against the freshly fetched remote git diff origin/main...HEAD # 5) Integrate — your choice, no surprises: git merge --ff-only origin/main # safe fast-forward only git rebase origin/main # replay your work on top git reset --hard origin/main # ⚠️ throws away local commits (own them first) # 6) Fetch a single branch by name (less data over the wire) git fetch origin feature-checkout:refs/remotes/origin/feature-checkout # 7) Pull = fetch + merge (or +rebase if you set pull.rebase true) git config --global pull.rebase true git config --global fetch.prune true
Why it matters
Get into the habit of `git fetch -p` first thing each day. It is read-only and reveals what teammates pushed overnight without committing you to any merge or rebase yet, so you can pick the right integration strategy per branch.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Example
Example
git fetch # update remote refs, don't merge git fetch --prune # drop stale remote branchesTry it Yourself »
Discussion
Loading…