Sub topic 12
Pulling with rebase instead of merge
This chapter assumes you can use git fetch, git pull, and git push, read git log output, and know what origin/main refers to. It also assumes you have met the basic rebase from earlier chapters.
What a plain pull does
git pull is a git fetch followed by an integration step. By default, that step is a merge. If you have local commits and the remote branch has new ones, the histories have diverged. Git then creates a merge commit that joins them.
On a branch you update often, this adds many small merge commits such as "Merge branch 'main' of origin". They carry no meaning of their own and clutter the log.
Pulling with rebase
git pull --rebase fetches, then replays your local commits on top of the upstream branch. The result is a straight line of commits and no merge commit.
git pull --rebase
git config --global pull.rebase trueThe first command does the pull once. The second makes rebase the default behavior of git pull for every repository on your machine.
The command is roughly equivalent to this sequence, assuming your branch tracks origin/feature:
git fetch origin
git rebase origin/featureWhy this is safe in the usual case
The commits being rewritten are your local, unpushed ones. Nobody else has them yet, so the shared-history rule is not broken. The commits you pull in from upstream are not changed.
The risk appears when your local branch contains commits you already pushed and others have based work on. Rebasing those gives them new hashes, and you will need --force-with-lease to push. Use git pull --rebase mainly on branches where you are the only one who has your local commits.
Conflicts
Conflicts work as in any rebase. Git stops at the commit that conflicts, and you resolve it in the same way.
git pull --rebase
# CONFLICT reported: edit the conflicted files, then
git add path/to/file
git rebase --continue
# to give up and return to the state before the pull:
git rebase --abortAborting returns your branch to where it was before the pull started.
Common errors
- Uncommitted changes. Rebase refuses to start with a dirty working tree. Commit or stash first, or use
git pull --rebase --autostash. - Surprise after switching the default. With
pull.rebase true, a pull that used to merge now rewrites your local commits. If you want a merge once, rungit pull --no-rebase. - Rebasing merges. If your local history contains merge commits, a plain rebase flattens them. A later chapter covers
--rebase-mergesfor this case. - Wrong upstream. If the branch has no upstream configured, Git cannot tell what to rebase onto. Set it with
git branch --set-upstream-to=origin/feature.
Exercise
You committed twice locally on feature without pushing. A teammate pushed one new commit to origin/feature. After git pull --rebase, how many commits of yours have new hashes, and is a merge commit created?
Show answer
Both of your commits get new hashes, because they are replayed on top of the teammate's commit. No merge commit is created, and the history is a single line.
Exercise
Which command makes git pull rebase by default for all your repositories?
Show answer
git config --global pull.rebase true