Sub topic 11
Setting how git pull reconciles history
This chapter assumes you have Git installed, you are comfortable in a terminal, and you know what a repository, a commit and a remote are. It also assumes you can already run git config at the global scope, as covered in the earlier chapters.
What git pull does
git pull is two commands in one. It first runs git fetch, which downloads new commits from the remote. It then integrates those commits into your current branch.
Integration is where Git must choose a strategy. If only the remote has new commits, Git can move your branch pointer forward (a fast-forward). If both your local branch and the remote have new commits, the histories have diverged. Git must then either merge them or rebase your commits on top of the remote ones.
Why you should set it explicitly
Recent Git versions warn when a pull needs a real merge or rebase and you have not chosen a behavior. The message says Git does not know how to reconcile divergent branches, and the pull stops. Setting pull.rebase once removes the prompt and makes the behavior predictable.
Choosing a value
The key is pull.rebase. These are the values you will use most often.
| Value | Behavior on divergent history | Resulting history |
|---|---|---|
| `false` | Merge the remote changes into your branch | A merge commit appears |
| `true` | Rebase your local commits onto the remote branch | Linear, with rewritten local commits |
| `merges` | Rebase but preserve merge commits you made locally | Mostly linear, merges kept |
For a first-time setup, the safest choice is merging:
git config --global pull.rebase falseMerging never rewrites commits you have already made. A rebase creates new commits to replace your local ones. That is harmless for unpushed work, but it can confuse you if a rebase stops on a conflict and you have not seen one before.
If you prefer a linear history, switch to rebase:
git config --global pull.rebase trueFast-forward only
A third approach refuses to create anything automatically:
git config --global pull.ff onlyWith pull.ff only, git pull succeeds only when a fast-forward is possible. If the history has diverged, Git stops and tells you. You then decide whether to run git merge or git rebase yourself. This suits people who want to see every divergence.
Overriding for a single command
Command-line options take precedence over configuration. You can set a default and still choose differently for one pull:
git pull --rebase
git pull --no-rebase
git pull --ff-onlyYou can also set the behavior for one repository only by using --local instead of --global, which writes to .git/config. Repository settings override global ones, as described in the precedence chapter.
Checking what is in effect
Confirm the value and the file it came from:
git config --show-origin --get pull.rebaseIf the command prints nothing and exits with an error status, the key is not set at any scope, and Git will prompt or warn on the next divergent pull.
Common mistakes
- Setting
pull.rebaselocally in one repository and forgetting that other repositories still use the global value. - Assuming
pull.rebase falseprevents merge commits. It does the opposite: a divergent pull creates one. - Expecting
pull.ff onlyto resolve a divergence. It only refuses.
Exercise
You run git pull and Git stops with a message that it cannot reconcile divergent branches. You want future pulls to rebase your local commits onto the remote branch in every repository on this machine. Which command do you run, and how do you verify which file supplies the value?
Show answer
Run git config --global pull.rebase true. Verify with git config --show-origin --get pull.rebase, which prints the value and the file path, normally ~/.gitconfig.
Exercise
A teammate wants git pull to fail whenever history has diverged, so they can choose between merge and rebase manually. Which setting gives this behavior?
Show answer
git config --global pull.ff only, with pull.rebase left unset or set to false. A pull then succeeds only as a fast-forward.