Sub topic 6
Merging with a fast-forward
This chapter assumes you can commit, read git log, and create and switch branches with git switch. It covers the simplest kind of merge, where Git only moves a pointer.
What a fast-forward is
A merge brings the commits of one branch into another. If the branch you are on has not gained any commits since the other branch split off, Git has no work to combine. The target branch's history is a straight line to the other branch's tip. Git moves the branch pointer forward along that line. This is a fast-forward merge. It creates no new commit.
Running one
Run the merge from the branch that should receive the changes. Here that is main.
git switch main
git merge feature/login
git log --oneline --graph --decorateIf main has not moved since you created feature/login, Git prints a line like Fast-forward, followed by a summary of the changed files. The log shows one straight line. main and feature/login both appear on the newest commit.
Reading the result
After a fast-forward:
mainpoints at the same commit asfeature/login.- No merge commit exists, so the history looks as if you had committed directly on
main. - The old branch name still exists. It is now redundant and can be deleted.
When it does not apply
Git can only fast-forward when the current branch's tip is an ancestor of the branch being merged. If someone added a commit to main after you branched, the two histories have diverged. Git then falls back to a three-way merge, covered in the next chapter.
You can check before merging. This command prints the commit where the two branches split:
git merge-base main feature/loginIf the printed commit is the same as main's tip, a fast-forward is possible.
Controlling the behavior
Two options change the default.
| Option | Effect |
|---|---|
| `--ff-only` | Merges only if a fast-forward is possible. Otherwise Git stops and changes nothing. |
| `--no-ff` | Always creates a merge commit, even when a fast-forward is possible. |
--no-ff keeps a visible record that a group of commits belonged to one branch. --ff-only is a useful safeguard when you want a linear history and prefer an error over an unexpected merge commit.
git merge --ff-only feature/loginCommon errors
- Merging from the wrong branch.
git merge feature/loginmerges into whatever branch is checked out. Rungit branchorgit statusfirst. - Expecting a merge commit. A fast-forward leaves none, so
git logwill not show a "Merge branch" entry. fatal: Not possible to fast-forward, aborting.This appears with--ff-onlywhen the branches have diverged. Rebase the feature branch ontomain, or drop the option and accept a merge commit.
Exercise
You are on main. You created feature/login from main, added two commits to it, and have not touched main since. What does git merge feature/login do, and does it create a new commit?
Show answer
It fast-forwards. Git moves the main pointer to the tip of feature/login, so main now includes both commits. No new commit is created, because main's tip is an ancestor of feature/login and there is nothing to combine.
Exercise
A teammate pushed a commit to main after you branched. You run git merge --ff-only feature/login on main. What happens, and what can you do?
Show answer
Git aborts with "Not possible to fast-forward" and changes nothing, because main and feature/login have diverged. You can rebase feature/login onto main and merge again, or merge without --ff-only to get a merge commit.