Sub topic 9
Pulling: fetch plus integrate
This chapter assumes you can commit, create and switch branches, and know what merging and rebasing do. It also assumes you have met git fetch and remote-tracking branches such as origin/main.
What pull does
git pull is two commands in one:
git fetchdownloads new commits and updates the remote-tracking branches, such asorigin/main.- Git then integrates the upstream branch into your current branch, by merge or by rebase.
Only your current branch changes. The integration step is what makes pull riskier than fetch. It can create a merge commit, rewrite your local commits, or stop on conflicts.
Which branch gets pulled
A bare git pull uses the upstream of your current branch. You set the upstream with git push -u, or it is set for you when you check out a branch from a remote. To see it, run:
git branch -vvIf the branch has no upstream, git pull fails with a message that there is no tracking information. In that case, name the remote and branch yourself:
git pull origin mainThree outcomes
After the fetch, your branch and its upstream are in one of three states:
| State | What pull does |
|---|---|
| You have no new local commits | Fast-forward: your branch pointer moves forward. No new commit. |
| Only the remote has new commits | Same as above. |
| Both sides have new commits (diverged) | Merge creates a merge commit. Rebase replays your commits on top. |
Divergence is the case that needs a decision. The next chapter section covers the strategies in detail. This chapter shows how pull behaves with each.
Controlling the integration step
You choose the strategy with a flag or a config setting:
git pull --ff-only
git pull --rebase
git config --global pull.rebase true--ff-onlyrefuses to do anything except a fast-forward. If your history diverged, the pull stops and changes nothing. This is the safest option, because you then decide how to integrate.--rebasereplays your local commits on top of the fetched ones. History stays linear, and your commit hashes change.pull.rebase truemakes rebase the default for every pull.
Recent Git versions print a warning when you pull a diverged branch with no strategy configured. Newer versions refuse to continue until you pick one. Setting pull.rebase or pull.ff once avoids the prompt.
A safer habit: fetch, look, then integrate
Pull hides the moment where you could look at incoming work. When you are unsure, split it up:
git fetch origin
git log --oneline main..origin/main
git merge --ff-only origin/mainThis is the same result as git pull --ff-only, but you saw the incoming commits first.
Common errors
- "You have unstaged changes" during a rebase pull. Rebase needs a clean working tree. Commit or stash first, or use
git pull --rebase --autostash. - "fatal: Not possible to fast-forward, aborting." You used
--ff-onlyand the history diverged. Fetch, then merge or rebase deliberately. - Conflicts in the middle of a pull. A merge pull leaves conflict markers in files. Fix them, run
git add, and thengit commit. A rebase pull stops at each conflicting commit. Fix the files, rungit add, and thengit rebase --continue. To back out, usegit merge --abortorgit rebase --abort. - Pulling on the wrong branch. Pull changes whichever branch is checked out. Check with
git statusfirst.
Exercise
Your branch main has two local commits that are not pushed. origin/main has three new commits. You run git pull --ff-only. What happens, and what could you run next to finish the job?
Show answer
The pull fails because the histories diverged, so a fast-forward is impossible. Nothing changes except that the fetch updated origin/main. You can then run git rebase origin/main to put your two commits on top, or git merge origin/main to create a merge commit.