Git Pull from GitHub
When the remote has commits you don't have, you need to bring them into your local branch. git pull does that in one command, but it is really two:
git pull = git fetch + git merge origin/main (or git rebase origin/main)Knowing both halves helps you understand what pull is doing, and what to do when it can't.
Every example's starting point
Each example starts with this setup: a "GitHub" bare repository, your clone in ~/shop, and a colleague's clone in ~/colleague (which could just as well be the GitHub web editor):
git init -q --bare ~/github/shop.git
mkdir shop && cd shop && git init -q
printf 'tea\n' > menu.txt && git add . && git commit -q -m "Start menu"
git remote add origin ~/github/shop.git && git push -q -u origin main
git clone -q ~/github/shop.git ~/colleagueFetch, then merge
git init -q --bare ~/github/shop.git
mkdir shop && cd shop && git init -q
printf 'tea\n' > menu.txt && git add . && git commit -q -m "Start menu"
git remote add origin ~/github/shop.git && git push -q -u origin main
git clone -q ~/github/shop.git ~/colleague
cd ~/colleague
echo "coffee" >> menu.txt && git commit -q -am "Add coffee" && git push -q
cd ~/shop
git fetch
git status -sb
git merge origin/main
cat menu.txtFrom /home/learner/github/shop
f7bb141..f0da3fe main -> origin/main
## main...origin/main [behind 1]
Updating f7bb141..f0da3fe
Fast-forward
menu.txt | 1 +
1 file changed, 1 insertion(+)
tea
coffee
Because you had no local commits, the merge was a fast-forward.
Pull in one step
git init -q --bare ~/github/shop.git
mkdir shop && cd shop && git init -q
printf 'tea\n' > menu.txt && git add . && git commit -q -m "Start menu"
git remote add origin ~/github/shop.git && git push -q -u origin main
git clone -q ~/github/shop.git ~/colleague
cd ~/colleague
echo "coffee" >> menu.txt && git commit -q -am "Add coffee" && git push -q
cd ~/shop
git pull
git log --onelineFrom /home/learner/github/shop
f7bb141..f0da3fe main -> origin/main
Updating f7bb141..f0da3fe
Fast-forward
menu.txt | 1 +
1 file changed, 1 insertion(+)
f0da3fe Add coffee
f7bb141 Start menu
When both sides have new commits
If you committed locally and the remote moved, the histories have diverged. Git cannot fast-forward, and since Git 2.27 git pull asks you to choose how to combine them:
git init -q --bare ~/github/shop.git
mkdir shop && cd shop && git init -q
printf 'tea\n' > menu.txt && git add . && git commit -q -m "Start menu"
git remote add origin ~/github/shop.git && git push -q -u origin main
git clone -q ~/github/shop.git ~/colleague
cd ~/colleague
echo "coffee" >> menu.txt && git commit -q -am "Add coffee" && git push -q
cd ~/shop
echo "open 9-17" > hours.txt && git add . && git commit -q -m "Add opening hours"
git pull
echo "exit status: $?"From /home/learner/github/shop
f7bb141..f0da3fe 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 three options:
| Option | Result |
|---|---|
git pull --no-rebase (pull.rebase false) |
a merge commit joins the two lines; history shows the parallel work |
git pull --rebase (pull.rebase true) |
your local commits are replayed on top of the remote ones; history stays linear |
git pull --ff-only (pull.ff only) |
only fast-forward; stop if the histories diverged |
Set your preference once, for example git config --global pull.rebase true. Here are the merge and the rebase versions side by side:
setup() {
rm -rf ~/github ~/shop ~/colleague
git init -q --bare ~/github/shop.git
mkdir ~/shop && cd ~/shop && git init -q
printf 'tea\n' > menu.txt && git add . && git commit -q -m "Start menu"
git remote add origin ~/github/shop.git && git push -q -u origin main
git clone -q ~/github/shop.git ~/colleague
cd ~/colleague && echo "coffee" >> menu.txt && git commit -q -am "Add coffee" && git push -q
cd ~/shop && echo "open 9-17" > hours.txt && git add . && git commit -q -m "Add opening hours"
}
setup
git pull -q --no-rebase
echo "== pull --no-rebase:"
git log --oneline --graph
setup
git pull -q --rebase
echo "== pull --rebase:"
git log --oneline --graph== pull --no-rebase:
* 67d9c71 Merge branch 'main' of /home/learner/github/shop
|\
| * f0da3fe Add coffee
* | 3b25917 Add opening hours
|/
* f7bb141 Start menu
== pull --rebase:
* 8d2c71b Add opening hours
* f0da3fe Add coffee
* f7bb141 Start menu
With --rebase, "Add opening hours" got a new hash: it was re-created on top of "Add coffee".
Note: If the same lines were changed on both sides, pull stops with a merge conflict, just like a local merge. Fix the files,
git addthem, thengit commit(merge) orgit rebase --continue(rebase).
Test yourself
Which two commands does git pull combine by default?
- git fetch + git merge
- git push + git fetch
- git clone + git checkout
- git commit + git push
git pull stops with "Need to specify how to reconcile divergent branches". What has happened?
- The remote is offline
- Both your branch and the remote have new commits, and no pull strategy is configured
- You have a merge conflict
- Your branch has no upstream
What is the advantage of git pull --rebase?
- It never conflicts
- It keeps history linear by replaying your local commits on top of the remote ones
- It deletes remote commits
- It pushes automatically