Fetching and Pulling: Remote-Tracking Branches, Merge or Rebase, and Divergent Branches
How to get other people's work: git fetch and remote-tracking branches, ahead and behind, git pull as fetch plus merge or rebase, choosing pull.rebase or pull.ff, divergent branches, pulling with local changes, pruning, fetching all remotes, and resetting to match the remote.
kvi5hnuCode Sprout team member8 min readOn this page
When teammates push commits, your repository doesn't change by itself. Two commands bring their work in: git fetch downloads new commits and updates your remote-tracking branches, without touching your own branches or files, and git pull fetches and then integrates the changes into your current branch. Understanding the difference, and choosing how pull integrates, prevents most surprises when working with others.
The examples simulate a team: a bare repository plays the server, and two clones, ana and sam, play two developers.
Fetch: download without changing your work

git init -q --bare server.git
git clone -q server.git ana 2>/dev/null; cd ana
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"; git push -q origin main
cd ..; git clone -q server.git sam
cd ana # Ana pushes a new commit
echo "Lake path: 5 km" > lake.txt; git add .
git commit -q -m "Add lake page" --author="Ana Diaz <ana@example.com>"; git push -q
cd ../sam
echo "=== before fetching, Sam's repository doesn't know"
git status -sb
git fetch
echo "=== after fetching"
git status -sb
echo "=== what's new on the remote"
git log --oneline main..origin/main
git diff --stat main origin/main
echo "=== Sam's files are unchanged"
ls
=== before fetching, Sam's repository doesn't know
## main...origin/main
From /workspace/server
7fb62a0..3d9f814 main -> origin/main
=== after fetching
## main...origin/main [behind 1]
=== what's new on the remote
3d9f814 Add lake page
lake.txt | 1 +
1 file changed, 1 insertion(+)
=== Sam's files are unchanged
README.md
git fetch printed 7fb62a0..3d9f814 main -> origin/main: the remote's main moved, so Git moved origin/main. Sam's main and working tree didn't change. git status now says the branch is [behind 1]. Fetching is always safe: you can fetch as often as you like and then decide what to do with what arrived.
Useful forms:
| Command | Fetches |
|---|---|
git fetch | from the current branch's remote, usually origin: all branches and their tags |
git fetch upstream | from another remote |
git fetch --all | from every remote |
git fetch origin main | only main (updating origin/main) |
git fetch --prune (-p) | and deletes remote-tracking branches whose branch was deleted on the remote |
git fetch --tags | and all tags, even ones not on fetched branches |
git fetch --dry-run | nothing: shows what would be updated |
Ahead, behind and diverged
Git compares your branch with its upstream, the remote-tracking branch it was set up to follow, usually origin/<same name>:
git status says | Meaning | What to do |
|---|---|---|
| up to date | same commit | nothing |
| ahead by N | you have N commits the remote doesn't | git push |
| behind by N | the remote has N commits you don't | git pull, which will fast-forward |
| have diverged | both have new commits | merge or rebase, then push |
These counts are only as current as your last fetch: "up to date" means up to date with what you last fetched. git status doesn't contact the remote.
Pull: fetch, then integrate
git pull runs git fetch for the current branch's upstream, then integrates the upstream into your branch, by default with a merge. When you have no new commits of your own, the merge is a fast-forward:
git init -q --bare server.git
git clone -q server.git ana 2>/dev/null; cd ana
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"; git push -q origin main
cd ..; git clone -q server.git sam
cd ana; echo "Lake path: 5 km" > lake.txt; git add .
git commit -q -m "Add lake page" --author="Ana Diaz <ana@example.com>"; git push -q; cd ../sam
git pull
git log --oneline
From /workspace/server
7fb62a0..3d9f814 main -> origin/main
Updating 7fb62a0..3d9f814
Fast-forward
lake.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 lake.txt
3d9f814 Add lake page
7fb62a0 Start project
When both sides have new commits, the branches have diverged, and there are two ways to combine them. Current Git won't guess which you want:
git init -q --bare server.git
git clone -q server.git ana 2>/dev/null; cd ana
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"; git push -q origin main
cd ..; git clone -q server.git sam
cd ana; echo "Ridge loop: 8 km" > ridge.txt; git add .
git commit -q -m "Add ridge page" --author="Ana Diaz <ana@example.com>"; git push -q; cd ../sam
echo "body { margin: 0 }" > style.css; git add .; git commit -q -m "Add stylesheet"
git pull
echo "exit status: $?"
From /workspace/server
7fb62a0..4e9111d main -> origin/main
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint: git config pull.rebase false # merge
hint: git config pull.rebase true # rebase
hint: git config pull.ff only # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.
exit status: 128
The choices it lists:
git pull --no-rebase, orpull.rebase=false: merge. Creates a merge commit, "Merge branch 'main' of ...", joining your commits and theirs.git pull --rebase, orpull.rebase=true: rebase. Replays your new commits on top of the fetched ones, keeping the history linear (article 14).git pull --ff-only, orpull.ff=only: only fast-forward, refusing when you have diverged, so you decide each time.
git init -q --bare server.git
git clone -q server.git ana 2>/dev/null; cd ana
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"; git push -q origin main
cd ..; git clone -q server.git sam
cd ana; echo "Ridge loop: 8 km" > ridge.txt; git add .
git commit -q -m "Add ridge page" --author="Ana Diaz <ana@example.com>"; git push -q; cd ../sam
echo "body { margin: 0 }" > style.css; git add .; git commit -q -m "Add stylesheet"
git pull --rebase -q
git log --oneline --graph
git status -sb
git push -q && echo "pushed"
* 3dd0807 Add stylesheet
* 4e9111d Add ridge page
* 7fb62a0 Start project
## main...origin/main [ahead 1]
pushed
Sam's commit was replayed on top of Ana's, so the history is a straight line and the push succeeds.
Which to choose
For updating your own branch with others' work, rebasing is usually better: routine git pull merges produce "Merge branch 'main' of github.com/..." commits that add noise and record nothing useful. Many developers set it once:
git config --global pull.rebase true
git config --global rebase.autoStash true # stash local edits around the rebase
Use merging when your local commits are already shared, for example pushed to another branch others use, since rebasing would rewrite them. Teams that want to be explicit set pull.ff=only and run git rebase or git merge deliberately when a fast-forward isn't possible.
pull.rebase=merges rebases while keeping any merge commits you made locally. You can also set the behaviour per branch with branch.<name>.rebase, and branch.autoSetupRebase=always sets it on new branches automatically.
If a pull's rebase or merge stops with conflicts, resolve them as in articles 13 and 14, then git rebase --continue or git merge --continue. git rebase --abort or git merge --abort returns to where you were before the pull, with the fetched commits still in origin/main.
Pulling with uncommitted changes
If your uncommitted changes touch files the pull changes, Git refuses rather than overwrite them: "Your local changes to the following files would be overwritten by merge". Options:
- Commit first, then pull.
git pull --rebase --autostashstashes the changes, pulls, and reapplies them. Setrebase.autoStash=trueto do this automatically.git stash,git pull,git stash popdoes the same by hand (article 27).
Changes to files the pull doesn't touch don't get in the way.
Fetch then merge: the manual pull
Running the two steps yourself lets you look before integrating, which is useful before merging someone's branch:
git fetch origin
git log --oneline main..origin/main # what arrived
git diff main...origin/main # what it changes
git merge origin/main # or: git rebase origin/main
You can also pull a specific branch from a specific remote: git pull origin lake fetches lake and merges it into the current branch. That's rarely what you want on a feature branch, so prefer fetching and merging explicitly.
Matching the remote exactly
Sometimes you want your branch to be exactly what's on the remote, throwing away your local commits and changes, for example on a test machine:
git fetch origin
git reset --hard origin/main
This discards uncommitted changes permanently, and moves your branch off your local commits. Those can still be recovered with git reflog for a while (article 26). Add git clean -fd to also delete untracked files (article 28).
Pruning deleted branches
When a branch is deleted on the remote, for example after its pull request is merged, your origin/<branch> stays until you prune:
git init -q --bare server.git
git clone -q server.git ana 2>/dev/null; cd ana
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"
git push -q origin main
git switch -q -c lake; git commit -q --allow-empty -m "Lake work"; git push -q origin lake
cd ..; git clone -q server.git sam
git -C ana push -q origin --delete lake # Ana deletes the branch on the server
cd sam
echo "=== Sam still sees origin/lake"
git branch -r
git fetch --prune
echo "=== after git fetch --prune"
git branch -r
=== Sam still sees origin/lake
origin/HEAD -> origin/main
origin/lake
origin/main
From /workspace/server
- [deleted] (none) -> origin/lake
=== after git fetch --prune
origin/HEAD -> origin/main
origin/main
Set fetch.prune=true to prune on every fetch. Pruning never deletes your local branches, even ones that tracked the deleted branch. Article 23 covers finding and cleaning those up.
What's current
- When branches have diverged,
git pullrefuses until you choose merge, rebase or fast-forward only, through--rebase,--no-rebase,--ff-only, orpull.rebaseandpull.ff. fetch.pruneandrebase.autoStashare popular defaults.fetch.all=true(Git 2.44) makes plaingit fetchfetch from every remote.- Background maintenance (
git maintenance start) can prefetch from remotes hourly, sogit fetchitself is quick (article 44). Prefetched commits go to a separate place and don't move your remote-tracking branches.
Common questions
What's the difference between git fetch and git pull?
git fetch downloads commits and updates remote-tracking branches like origin/main. Nothing else changes. git pull does that and then merges or rebases the upstream into your current branch, changing your branch and files.
Is git pull dangerous?
Not in the sense of losing committed work. But it changes your branch, and with diverged branches it creates a merge or a rebase that you might not have intended. Choosing pull.rebase or pull.ff=only makes its behaviour predictable.
Why does git status say "up to date" when I know someone pushed?
git status compares with your last fetch and doesn't contact the server. Run git fetch first.
Why did git pull create a merge commit I didn't want?
Your branch and the remote had diverged, and pull was set to merge. Undo it with git reset --hard ORIG_HEAD if it's not pushed, then git pull --rebase. Set pull.rebase=true to make that the default.
How do I pull a branch I don't have locally?
git fetch, then git switch <branch>, which creates a local branch from origin/<branch> (article 18).
Summary
git fetchdownloads new commits and moves remote-tracking branches such asorigin/main, without changing your branches or files.git statusreports ahead, behind or diverged relative to your last fetch.git pullis fetch followed by merge or rebase of the upstream into the current branch. Without new local commits, it fast-forwards.- With divergent branches, choose
--rebase(linear),--no-rebase(merge commit) or--ff-only, or setpull.rebaseorpull.ff. --autostashhandles local changes,git reset --hard origin/mainmatches the remote exactly, andgit fetch --pruneremoves stale remote-tracking branches.
Practice questions
After git fetch, git status says your branch is "behind 'origin/main' by 3 commits". What's the quickest way to update, and will it create a merge commit?
Answer
git pull, or git merge origin/main since you've already fetched. Your branch has no commits the remote lacks, so it's a fast-forward: main moves to origin/main, with no merge commit, unless you've set pull.ff=false (the same as --no-ff).
You have two local commits, and a teammate pushed one commit to main. You want a linear history before pushing. Which command, and what happens to your commits?
Answer
git pull --rebase. It fetches the teammate's commit and replays your two commits on top of it, giving them new IDs. Then git push succeeds as a fast-forward on the server.
A test server's checkout has local edits and a stray commit, and you want it to match origin/main exactly. Which commands, and what's lost?
Answer
git fetch origin
git reset --hard origin/main
git clean -fd # if untracked files should go too
Uncommitted changes, and untracked files if you run git clean, are lost for good. The stray commit is no longer on the branch, but git reflog can still find it for a while.
Previous: Cloning Repositories: Branches, Shallow, Partial and Sparse Clones
Next: Pushing: Upstream Branches, push.default, Force-With-Lease, Deleting and Refspecs

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.