Sub topic 6
Merging with a fast-forward
This chapter covers the simplest kind of merge, the fast-forward. It assumes you can already commit, create branches with git switch -c, and read git log. You should also know that a branch is a pointer to a commit and that HEAD points at the branch you have checked out.
What a fast-forward is
A fast-forward merge happens when the branch you are merging into has not moved since the other branch split from it. The current branch's tip is an ancestor of the branch you are merging. Git has no history to combine. It only has to move the current branch pointer forward along a line that already exists.
No new commit is created. The commits on the feature branch become reachable from main with their original hashes.
When Git can fast-forward
Git can fast-forward when one condition holds: the commit that HEAD points to is an ancestor of the commit you are merging. You can check this yourself before merging:
git merge-base --is-ancestor main feature/login && echo "fast-forward possible"The command prints nothing and exits with status 0 when main is an ancestor of feature/login. It exits with status 1 otherwise. The echo runs only on success.
If main has gained commits of its own since you branched, the two branches have diverged. Git then needs a three-way merge, which is the next chapter's topic.
A worked example
Start from a repository where main has some commits. Create a branch, add two commits, and merge it back.
git switch -c feature/login
echo "login form" > login.txt
git add login.txt
git commit -m "Add login form"
echo "validation" >> login.txt
git commit -am "Validate login input"
git switch main
git merge feature/login
git log --oneline --graph --decorateThe merge prints something like this:
Updating 3f2a1c9..b81d4e7
Fast-forward
login.txt | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 login.txtThe first line gives the old and new tip of main. The word Fast-forward confirms that Git only moved the pointer. The log then shows a straight line with both branch names on the same commit:
* b81d4e7 (HEAD -> main, feature/login) Validate login input
* 7c0e5a2 Add login form
* 3f2a1c9 Previous work on mainBoth names point at b81d4e7. Nothing is copied. Two labels now sit on one commit.
What changes on disk
A fast-forward does two things:
- It moves the
mainref to the target commit. Under the hood this rewrites the file.git/refs/heads/main. - It updates your index and working tree to match the new tip, so the files from the feature branch appear.
Git refuses to start if the update would overwrite uncommitted changes in files that differ between the two commits. In that case, commit or stash first.
Controlling the behavior
By default git merge fast-forwards whenever it can. Three options change this.
| Option | Effect |
|---|---|
| `--ff` | Default. Fast-forward if possible, otherwise create a merge commit. |
| `--no-ff` | Always create a merge commit, even when a fast-forward is possible. |
| `--ff-only` | Fast-forward or fail. Never creates a merge commit. |
Forcing a merge commit with --no-ff
A merge commit records that a group of commits belonged together as one line of work. Some teams want that record, so they use --no-ff:
git switch main
git merge --no-ff feature/loginGit opens your editor for the merge message. The history now shows a bubble, and git log --first-parent main lists only the merge commits on the main line. The cost is extra merge commits in a history that would otherwise be linear.
Refusing anything but a fast-forward with --ff-only
--ff-only is useful in scripts and for keeping history linear:
git merge --ff-only feature/loginIf the branches have diverged, Git stops with an error and changes nothing:
fatal: Not possible to fast-forward, aborting.You can make this the default for git pull, which prevents surprise merge commits when you update a local branch:
git config --global pull.ff onlyYou can also set merge.ff to false (the same as always using --no-ff) or to only, for every merge in a repository.
Fast-forwarding without checking out the target
The merge happens in the branch you have checked out. To fast-forward a branch that is not checked out, such as a local main that is behind origin/main, you can fetch directly into it:
git fetch origin main:mainGit updates local main only if the change is a fast-forward. It refuses if you currently have main checked out. This avoids a branch switch when you only want to bring main up to date.
Fast-forward and remote branches
The same idea applies when you update a branch from the remote. After git fetch, origin/main may be ahead of your main with no local commits of your own on main. Then git merge origin/main is a fast-forward. Likewise, git push succeeds only when it is a fast-forward of the remote branch. If the remote has commits you lack, Git rejects the push as non-fast-forward, and you must integrate first.
Common mistakes
- Expecting a merge commit. If you see
Fast-forwardand no editor opens, the merge worked. Use--no-ffif you wanted the commit. - Merging in the wrong direction.
git merge feature/loginchanges the branch you are on. Rungit branchorgit statusfirst. Mergingmaininto a feature branch fast-forwards the feature branch only if it has no commits of its own. - Undoing a fast-forward. There is no merge commit to revert. To go back, move the branch to its old position, for example
git reset --hard ORIG_HEADstraight after the merge. Use the old hash shown in theUpdatingline if you have lost track. Do this only for commits you have not pushed. - Assuming the feature branch is gone. The
feature/loginname still exists after the merge. Deleting it is a separate step covered later.
Choosing a style
A fast-forward gives a linear history that is easy to read and bisect. It loses the fact that the work happened on a branch. --no-ff keeps that fact at the price of extra commits. Neither is wrong. Pick one per project and apply it consistently, because mixing them makes the log harder to read.
Exercise
You are on main, and feature/search has three commits that branched from the current tip of main. You run git merge --no-ff feature/search. How does the result differ from git merge feature/search, and which command would fail if someone had also pushed a new commit to main that you had not yet integrated?
Show answer
Plain git merge feature/search fast-forwards. It moves main to the third feature commit and creates no new commit. --no-ff creates a new merge commit with two parents (the old main tip and the third feature commit), so the history shows a branch bubble. Neither command is a problem for a commit that only exists on the remote, because git merge works on local branches. If your local main had gained a commit of its own, git merge --ff-only feature/search would be the command that fails, since the branches would have diverged. The new commit on the remote only affects git push, which would be rejected as non-fast-forward until you integrate it.
Exercise
Write the command that checks whether main can be fast-forwarded to feature/login, and the command that merges only if a fast-forward is possible.
Show answer
git merge-base --is-ancestor main feature/login exits with status 0 when a fast-forward is possible. git merge --ff-only feature/login performs the merge and aborts with "Not possible to fast-forward" if the branches have diverged.