Sub topic 10
Choosing between merge, rebase, and fast-forward only
This chapter assumes you can commit, create branches, and have a basic idea of what merging and rebasing do. It also assumes you know that git pull is a git fetch followed by an integration step. Here you will choose which integration step runs.
Why the choice matters
When you pull, Git compares your local branch with its upstream branch. There are three possible situations:
- Your branch has no new commits, and the remote has some. Git can move your branch pointer forward. This is a fast-forward.
- The remote has no new commits you lack. There is nothing to do.
- Both sides have new commits. The history has diverged, and Git must combine the two lines of work.
Only the diverged case requires a decision. You can combine the lines with a merge commit, or replay your commits on top of the remote ones.
The three strategies
Merge (the default behavior)
git pull --no-rebaseGit creates a merge commit with two parents. Your original commits keep their hashes. The history shows exactly what happened, including the fact that work happened in parallel. The cost is extra merge commits, such as "Merge branch 'main' of ...", which add noise when you pull often.
Rebase
git pull --rebaseGit sets your local commits aside, moves your branch to the upstream tip, and replays your commits on top. The history stays a straight line. The replayed commits are new commits with new hashes, so rebase rewrites your local history. This is safe for commits you have not pushed. It becomes a problem only for commits others already have.
Fast-forward only
git pull --ff-onlyGit updates your branch only if a fast-forward is possible. If the history has diverged, the command fails and changes nothing. You then decide how to combine the work yourself, with git merge origin/main or git rebase origin/main.
| Strategy | Result on diverged history | Rewrites your commits | Best for |
|---|---|---|---|
| Merge | Merge commit | No | Keeping an exact record of parallel work |
| Rebase | Linear history | Yes (unpushed ones) | Short-lived local work on top of a shared branch |
| `--ff-only` | Error, no change | No | Branches where you should never have local commits, and cautious defaults |
Setting a default
Pass a flag for one pull, or set a default so you do not have to remember it:
git config --global pull.rebase true
git config --global pull.ff onlyIf you set neither and the history has diverged, recent Git versions stop and ask you to choose. They print a hint that lists these options. That message is the usual reason people first meet this setting.
Command-line flags override the configuration, so git pull --rebase still works when pull.ff is only.
Common errors
- "fatal: Not possible to fast-forward, aborting." You used
--ff-onlyand the history diverged. Rungit fetch, then choosegit merge origin/mainorgit rebase origin/main. - Conflicts in the middle of a rebase. Git replays commits one at a time, so you may resolve conflicts several times. Fix the files, run
git add, thengit rebase --continue. To give up, rungit rebase --abort. - Rebasing commits that are already pushed. Teammates who based work on the old commits will see duplicates or conflicts. Rebase only commits that exist solely on your machine, unless your team has agreed on a workflow for it.
A practical rule
Use --ff-only on branches you only read from, such as main in a project where you work on feature branches. Use --rebase for your own feature branch when you want a clean line. Use merge when the team wants a record of every integration or when the commits are already shared.
Exercise
Your teammate pushed a commit to origin/main. You also made one local commit on main and have not pushed it. You run git pull --ff-only. What happens, and what command would you run next to get a linear history?
Show answer
The pull fails with "Not possible to fast-forward" and changes nothing, because the history has diverged. Run git rebase origin/main (the fetch already happened as part of the pull) to replay your commit on top of the remote one. Then git push origin main will succeed as a fast-forward.
Exercise
What is the difference between git config --global pull.rebase true and passing --rebase to one pull?
Show answer
The configuration makes rebase the default for every plain git pull in all your repositories. The flag applies only to that one command, and it overrides the configured default.