Git Pull

Updated · 11 min read
mediumGit 2.53gitgit-pullpull-rebasedivergent-branchesinterview

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:

Terminal
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 --oneline
Output
badb379 (HEAD -> main, origin/main, origin/HEAD) Add the late fee
c460dae Add README and the term fee

Pull New Work

On Navika's computer, one command brings it in:

Terminal
cd ../../navika/fee-tracker
git pull
Output
From ../../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:

  1. The fetch: From ../../fee-tracker and the line that moved origin/main, exactly as in "Git Fetch".
  2. The merge: Updating ... Fast-forward and the changed files. Navika had no new commits, so main was simply moved forward.
Terminal
git log --oneline
cat fees.txt
Output
badb379 (HEAD -> main, origin/main, origin/HEAD) Add the late fee
c460dae Add README and the term fee
Term 1 fee: $12,000
Late fee: $250

git 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:

Terminal
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 pull
Output
From ../../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:

ChoiceCommand onceSetting for every pullResult
Mergegit pull --no-rebasegit config pull.rebase falseA merge commit joins the two lines
Rebasegit pull --rebasegit config pull.rebase trueYour commits are replayed on top of the server's, in a straight line
Fast-forward onlygit pull --ff-onlygit config pull.ff onlyRefuse whenever the branches have diverged

Add --global to a setting to use it in every repository.

Diverged: both sides have a commit the other lacks

Choose Merge

Terminal
git pull --no-rebase --no-edit
git log --oneline --graph
Output
Merge 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 fee

A 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:

Terminal
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 --rebase
Output
From ../../fee-tracker
   faca68c..bc35bee  main       -> origin/main
Successfully rebased and updated refs/heads/main.
Terminal
git log --oneline --graph -4
Output
* 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 fee

No 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

Terminal
git pull [options] [<remote> [<branch>]]
CommandIt is used to
git pullFetch and integrate the upstream branch
git pull --no-rebaseIntegrate with a merge
git pull --rebaseIntegrate by replaying your commits on top
git pull --ff-onlyIntegrate only if no merge or rebase is needed
git pull <remote> <branch>Pull a specific branch from a specific remote
git pull --autostashPut 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:

  1. git fetch, which downloads and updates origin/main, and writes .git/FETCH_HEAD.
  2. git merge FETCH_HEAD (with pull.rebase false or --no-rebase) or git 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
Terminal
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 pull
Output
From ../../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.

Quick check: Git pull

4 questions
1What does git pull do?
2When does Git show "Need to specify how to reconcile divergent branches"?
3What does git pull --rebase do with your local commits?
4Which setting makes every pull rebase?

Key Takeaways

Quick reference

CommandIt is used to
git pullFetch and integrate the upstream branch
git pull --no-rebaseIntegrate with a merge commit
git pull --rebaseIntegrate by rebasing your local commits
git pull --ff-onlyIntegrate only by fast-forward
git pull --autostashStash uncommitted changes around the pull
git config --global pull.rebase trueAlways rebase when pulling
git config --global pull.ff onlyNever create a merge commit when pulling

Was this tutorial helpful?

Lesson 37 of 106