Git Pull
In the previous lesson, the postman brought letters to your letterbox, and you read them before doing anything. Sometimes you don't need to inspect the post: you just want it brought in and put in its place. That's git pull.
git pull runs git fetch, and then immediately combines the fetched work into your current branch. When only your teammates have new commits, it simply moves your branch forward. When you and they have new commits, the two lines of work have to be joined, and there are two ways to do that: a merge or a rebase. Git 2.53 asks you to choose one instead of guessing.
In this lesson, you will pull new work, meet the "divergent branches" message, and choose how pull combines work, once or for good.
The Starting Point
As in "Git Fetch": the server fee-tracker.git, with Navika's and Advitha's clones. Advitha has pushed the late fee:
git init --bare --quiet fee-tracker.git
mkdir navika
cd navika
git init --quiet fee-tracker
cd fee-tracker
echo "Fee Tracker" > README.md
echo 'Term 1 fee: $12,000' > fees.txt
git add .
git commit --quiet -m "Add README and the term fee"
git remote add origin ../../fee-tracker.git
git push --quiet -u origin main
cd ../..
git clone --quiet fee-tracker.git advitha/fee-tracker
cd advitha/fee-tracker
git config user.name "Advitha Anamaneni"
git config user.email "advitha@example.com"
echo 'Late fee: $250' >> fees.txt
git commit --quiet -am "Add the late fee"
git push --quiet
git log --onelinebadb379 (HEAD -> main, origin/main, origin/HEAD) Add the late fee
c460dae Add README and the term feePull New Work
On Navika's computer, one command brings it in:
cd ../../navika/fee-tracker
git pullFrom ../../fee-tracker
c460dae..badb379 main -> origin/main
Updating c460dae..badb379
Fast-forward
fees.txt | 1 +
1 file changed, 1 insertion(+)The output has two parts, one for each step:
- The fetch:
From ../../fee-trackerand the line that movedorigin/main, exactly as in "Git Fetch". - The merge:
Updating ... Fast-forwardand the changed files. Navika had no new commits, somainwas simply moved forward.
git log --oneline
cat fees.txtbadb379 (HEAD -> main, origin/main, origin/HEAD) Add the late fee
c460dae Add README and the term fee
Term 1 fee: $12,000
Late fee: $250git pull with no arguments uses the current branch's upstream, here origin/main, which git push -u set up. Without an upstream, Git asks which branch to pull.
When Both Sides Have New Commits
Now both work at the same time. Advitha adds the hostel fee and pushes, and Navika, without pulling first, commits a README change:
cd ../../advitha/fee-tracker
echo 'Hostel fee: $9,000' >> fees.txt
git commit --quiet -am "Add the hostel fee"
git push --quiet
cd ../../navika/fee-tracker
echo "Run the app to see the fees." >> README.md
git commit --quiet -am "Explain how to run the app"
git pullFrom ../../fee-tracker
badb379..31cbefa 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.The fetch worked, but Git stops before combining: fatal: Need to specify how to reconcile divergent branches. Navika's main and origin/main have diverged: each has a commit the other doesn't. A fast-forward is impossible, and Git won't choose between merging and rebasing for you. Nothing has changed in her branch.
The hint lists the three choices:
| Choice | Command once | Setting for every pull | Result |
|---|---|---|---|
| Merge | git pull --no-rebase | git config pull.rebase false | A merge commit joins the two lines |
| Rebase | git pull --rebase | git config pull.rebase true | Your commits are replayed on top of the server's, in a straight line |
| Fast-forward only | git pull --ff-only | git config pull.ff only | Refuse whenever the branches have diverged |
Add --global to a setting to use it in every repository.
Choose Merge
git pull --no-rebase --no-edit
git log --oneline --graphMerge made by the 'ort' strategy.
fees.txt | 1 +
1 file changed, 1 insertion(+)
* faca68c (HEAD -> main) Merge branch 'main' of ../../fee-tracker
|\
| * 31cbefa (origin/main, origin/HEAD) Add the hostel fee
* | fdaa5fe Explain how to run the app
|/
* badb379 Add the late fee
* c460dae Add README and the term feeA merge commit, Merge branch 'main' of ../../fee-tracker, joins Navika's commit and Advitha's. This is the same merge you know from "Git Merge", with origin/main as the other branch. The history keeps both lines as they really happened.
Choose Rebase
The same situation again: Advitha pushes, and Navika commits locally:
git push --quiet
cd ../../advitha/fee-tracker
git pull --quiet --no-rebase
echo 'Exam fee: $800' >> fees.txt
git commit --quiet -am "Add the exam fee"
git push --quiet
cd ../../navika/fee-tracker
echo "Built by Navika." >> README.md
git commit --quiet -am "Credit the author"
git pull --rebaseFrom ../../fee-tracker
faca68c..bc35bee main -> origin/main
Successfully rebased and updated refs/heads/main.git log --oneline --graph -4* 3bb223a (HEAD -> main) Credit the author
* bc35bee (origin/main, origin/HEAD) Add the exam fee
* faca68c Merge branch 'main' of ../../fee-tracker
|\
| * 31cbefa Add the hostel feeNo merge commit this time. Git set Navika's new commit aside, moved main to the server's newest commit, and then replayed her commit on top: Successfully rebased and updated refs/heads/main. The history is one straight line, as if she had pulled before committing. Her commit got a new ID, because it now has a different parent. That is fine for commits you haven't pushed yet, which is exactly the situation when you pull. Rebasing has its own lessons.
Syntax
git pull [options] [<remote> [<branch>]]| Command | It is used to |
|---|---|
git pull | Fetch and integrate the upstream branch |
git pull --no-rebase | Integrate with a merge |
git pull --rebase | Integrate by replaying your commits on top |
git pull --ff-only | Integrate only if no merge or rebase is needed |
git pull <remote> <branch> | Pull a specific branch from a specific remote |
git pull --autostash | Put uncommitted changes aside during the pull and bring them back after |
Pulling with Uncommitted Changes
If you have edits that aren't committed, and the incoming commits change the same files, git pull stops with Your local changes to the following files would be overwritten by merge, just as git switch does. Commit or stash your changes, then pull. git pull --autostash (or the setting rebase.autoStash for rebasing pulls) does the stashing for you. If the incoming work changes the same lines as your new commits, the pull stops with a conflict, which you resolve exactly as in "Resolving Merge Conflicts".
Common Mistakes
Pulling with a messy working tree. Commit or stash first, so that a conflict or an undo is easy to handle.
Ignoring the "divergent branches" message. It's not an error in your repository. Pick merge or rebase with an option, or set pull.rebase once.
Using git pull --rebase after pushing your commits. Rebasing changes commit IDs. Pull with rebase only when the commits being replayed are still local, which is the normal case.
Pulling into the wrong branch. git pull integrates into the current branch. Check git branch --show-current first.
Never fetching or pulling. The longer you wait, the more you diverge, and the bigger the conflicts. Pull before you start work and before you push.
Under the Hood
git pull is literally the two commands, run one after the other:
git fetch, which downloads and updatesorigin/main, and writes.git/FETCH_HEAD.git merge FETCH_HEAD(withpull.rebase falseor--no-rebase) orgit rebase origin/main(with--rebase).
That is also why the merge commit's message names the remote and branch, Merge branch 'main' of ../../fee-tracker: the merge works from FETCH_HEAD, which records where the commit came from. The settings pull.rebase, pull.ff and branch.<name>.rebase only choose the second command.
Interview Corner
What does git pull do?
It runs git fetch to download new commits and update the remote-tracking branch, then integrates the current branch's upstream into the current branch, by fast-forward, merge or rebase depending on options and configuration.
What is the difference between git pull and git pull --rebase?
Plain git pull (with pull.rebase=false) merges, creating a merge commit when both sides have new commits. git pull --rebase replays your local commits on top of the fetched branch, keeping the history linear and giving the replayed commits new IDs.
What does "You have divergent branches and need to specify how to reconcile them" mean?
Your branch and its upstream both have commits the other doesn't, and Git has no configured preference for combining them. Choose per pull with --no-rebase, --rebase or --ff-only, or set pull.rebase (or pull.ff only) once.
Is it better to fetch and merge separately, or to pull?
git pull is convenient when you trust the incoming work. Fetching separately lets you inspect main..origin/main first, and choose how and when to integrate. Both end with the same result.
What does git pull --ff-only do?
It integrates only if the current branch can be fast-forwarded to the upstream, meaning you have no local commits. Otherwise it stops without changing anything.
Practice
Challenge: rebase by default. In Navika's clone, set pull.rebase to true for this repository. Then Advitha pushes one more commit, Navika commits one locally, and Navika runs a plain git pull.
What does Git print? Before you open the solution, decide whether the plain git pull stops with the divergent branches message, and whether a merge commit appears.
Show solution
git config pull.rebase true
git push --quiet
cd ../../advitha/fee-tracker
git pull --quiet
echo 'Sports fee: $700' >> fees.txt
git commit --quiet -am "Add the sports fee"
git push --quiet
cd ../../navika/fee-tracker
echo "Tested by Advitha." >> README.md
git commit --quiet -am "Note who tested the app"
git pullFrom ../../fee-tracker
3bb223a..9df79d1 main -> origin/main
Successfully rebased and updated refs/heads/main.No divergent branches message: the setting answered Git's question. The plain git pull rebased Navika's commit on top of Advitha's sports fee, and no merge commit was made. Check it with git log --oneline --graph -3.
Checkpoint. With two clones of one repository, commit in both, push from one, and pull in the other three times: once with --no-rebase, once with --rebase, and once with --ff-only. Compare the history after each with git log --oneline --graph.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git pull | Fetch and integrate the upstream branch |
git pull --no-rebase | Integrate with a merge commit |
git pull --rebase | Integrate by rebasing your local commits |
git pull --ff-only | Integrate only by fast-forward |
git pull --autostash | Stash uncommitted changes around the pull |
git config --global pull.rebase true | Always rebase when pulling |
git config --global pull.ff only | Never create a merge commit when pulling |
Was this tutorial helpful?