Sub topic 3
Merging: fast-forward and merge commits
This chapter assumes you know add, commit, push and pull, and that you understand a branch as a pointer to a commit. Here you will see the two ways Git joins branches, and how to choose between them.
What a merge does
git merge <branch> brings the commits of another branch into the branch you are on. Git first finds the merge base, the most recent commit that both branches share. What happens next depends on whether the current branch has moved since that point.
Fast-forward merges
If main has no new commits since the feature branch was created, the feature branch is simply ahead of main. Git has nothing to combine. It moves the main pointer forward to the feature tip. No new commit is created, and the history stays a straight line.
Merge commits
If both branches have new commits, their histories have diverged. Git creates a merge commit, a commit with two parents. One parent is the old tip of main, and the other is the tip of the feature branch. The merge commit records that the two lines of work were joined.
Forcing a merge commit with --no-ff
You can ask for a merge commit even when a fast-forward is possible. The --no-ff option does this, so the history shows that a branch existed.
git switch -c feature/login-form
# edit files
git add .
git commit -m "Add login form"
git switch main
git merge --no-ff feature/login-formWithout --no-ff, Git would fast-forward main here, because main has not moved. With it, Git adds a merge commit. Teams use this to group a feature's commits under one visible merge point. It also lets you undo the whole feature later by reverting that single merge commit.
Comparing the two
| Aspect | Fast-forward | Merge commit |
|---|---|---|
| New commit created | No | Yes |
| History shape | Linear | Shows branch and join |
| When it applies | Target has not moved | Always possible |
| Branch boundary visible | No | Yes |
| Config option | `--ff-only` requires it | `--no-ff` forces it |
Control and common errors
- Merge refused with
--ff-only. This option makes Git fail when a fast-forward is impossible. Rebase or merge the target into your branch first, then retry. - Unexpected merge commits on
git pull.git pullruns a fetch and then a merge. If your local branch has diverged from the remote, you get a merge commit nobody intended. Usegit pull --ff-onlyto see the divergence, orgit pull --rebaseto replay your commits instead. - Merging in the wrong direction. Run
git mergewhile on the branch that should receive the changes. Check withgit branch --show-currentfirst. - Conflicts. If both branches changed the same lines, Git pauses the merge and marks the files. Chapter 6 covers resolving these. To back out, run
git merge --abort.
Choosing between them
Use a fast-forward when you want a clean, linear history and the branch is short. Use a merge commit when the branch carried several related commits that you want to keep grouped, or when you may need to revert the feature as a unit. Later chapters compare these against squash and rebase.
Exercise
You created feature/search from main. Since then, a teammate pushed two commits to main, and you added three commits to your branch. You run git merge feature/search on main. Will this fast-forward? What would you see in the history?
Show answer
No. main has moved since the branch was created, so the histories have diverged and Git cannot just move the pointer. It creates a merge commit with two parents: the new tip of main and the tip of feature/search. git log --graph shows the two lines splitting and rejoining.