Merging: fast-forward and three-way

Beginner · 9 min

Combine branches with git merge, and see why Git sometimes just moves a pointer (fast-forward) and sometimes builds a two-parent merge commit (three-way).

Before thisCreating and switching branches

Cloud With Raj · 9 min

Merging: fast-forward and three-way

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Cloud With RajReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • git merge <branch> brings that branch's commits into the branch you are on.
  • When your branch has not moved since the other branch split off, Git fast-forwards: it just moves your branch pointer forward, creating no new commit.
  • When both branches have new commits, Git does a three-way merge using their common ancestor and creates a merge commit with two parents.
  • git merge --no-ff forces a merge commit even when a fast-forward is possible, and --ff-only refuses to merge unless it can fast-forward.
  • git merge-base shows the common ancestor, and git log --graph shows the shape of what the merge did.

What merging is for

You made a branch, did some work on it, and now you want that work on main. Merging combines the history of another branch into the one you’re on. The command is always run from the branch that receives: git switch main, then git merge search.

What Git does depends on the shape of the two histories, and there are two shapes.

Shape one: fast-forward

In tinynotes, main was at the “Move design notes” commit when I created search from it. I committed once on search and never touched main again. The merge base, the latest commit both branches share, is the tip of main itself:

$ git merge-base main search
ecbaa7f61acdfefb8dcaf1dc96befa3c012cb231

Nothing on main needs combining. search simply contains everything main has, plus one more commit. So Git moves main forward:

$ git merge search
Updating ecbaa7f..748069e
Fast-forward
 notes.py | 5 +++++
 1 file changed, 5 insertions(+)

That’s the whole operation. Fast-forward is Git’s word for it, and no new commit is created. The branch pointer from the branch lesson moved one step along the chain. The graph afterwards is a straight line, as if you had committed on main all along. Delete the finished branch with git branch -d search, which Git allows because it’s merged.

Shape two: diverged

Now both branches get new work. I created fix-count and made count print 0 when there is no notes file. Meanwhile on main I updated the README. Different files, so no clash, but both branches have a commit the other lacks:

$ git log --oneline --graph --all
* cef6114 Document the search command
| * 2d7ba21 Print 0 when there are no notes yet
|/  
* 748069e Add a search command
* ecbaa7f Move design notes into docs

They forked at 748069e. Moving a pointer can’t help, since main has cef6114 and fix-count has 2d7ba21 and neither contains the other. Git tells you so if you insist:

$ git merge --ff-only fix-count
hint: Diverging branches can't be fast-forwarded, you need to either:
...
fatal: Not possible to fast-forward, aborting.

--ff-only means “merge only if you can fast-forward”. It’s a useful guard, for example when you pull a shared branch and don’t want surprise merge commits.

Three-way merge

Without --ff-only, Git uses three versions of the project:

  1. the merge base, 748069e, where the branches split;
  2. ours, the tip of the branch you’re on;
  3. theirs, the tip of the branch you’re merging.

For each file, Git asks what each side changed relative to the base. A change on one side only is taken. A change to different parts of the file on both sides is combined. A change to the same lines on both sides is a conflict, and the next lesson deals with that. Comparing only two tips couldn’t tell “one side deleted this line” from “the other side added it”. The base is what makes that decidable.

$ git merge fix-count -m "Merge branch 'fix-count'"
Merge made by the 'ort' strategy.
 notes.py | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

ort is the name of the merge algorithm, the current default, and you can ignore it. Git has created a merge commit. Look at it:

$ git cat-file -p HEAD
tree e7540c8134b8d10b15130c20263ac65c893b7ca3
parent cef6114f44b6a32038e0e6dd3f4243fa3e9f38dc
parent 2d7ba2115817d1d82bedcc88d0d2320f692056d2
author Ada Example <ada@example.com> 1768816800 +0000
...
Merge branch 'fix-count'

Two parent lines, one per line of history it joins. That’s the only special thing about a merge commit. The graph shows the join:

$ git log --oneline --graph
*   3702e9b Merge branch 'fix-count'
|\  
| * 2d7ba21 Print 0 when there are no notes yet
* | cef6114 Document the search command
|/  
* 748069e Add a search command

git log --first-parent follows only the first parent at each merge, which reads as a summary of what landed on main and in what order.

I ran python3 notes.py count afterwards with no notes file and got 0, and search still worked, so both branches’ changes were present.

Forcing or forbidding the merge commit

Some teams like a merge commit for every feature, so the branch stays visible in history. For that, add --no-ff. In a scratch copy of the repository, I merged search that way:

$ git merge --no-ff search -m "Merge branch 'search'"
Merge made by the 'ort' strategy.
 notes.py | 5 +++++
 1 file changed, 5 insertions(+)

A merge commit with parents ecbaa7f and 748069e appeared, even though a fast-forward was possible. Whether to prefer one or the other is a team decision, and a later lesson compares merging with rebasing.

Merging a branch you already contain is harmless: “Already up to date.”

What the video covers

Cloud With Raj’s “Git Branch and Merge | Fast Forward Merge Vs Recursive Three Way Merge | with Quiz!” is about nine minutes and was published in December 2021. The title is this lesson’s two shapes, and the description says it explains branching and merging and the difference between fast-forward and recursive three-way merges. I couldn’t read a transcript. Note the word “recursive”: that was Git’s default three-way strategy for years, and current Git uses ort, as in the output above. The idea is the same. The description also promotes the creator’s courses. The channel is English-language, judged from the title and description. CherryPick Academy’s fast-forward video is linked for that half alone.

Where this fits

This merge worked cleanly because the branches touched different files. When they touch the same lines, Git stops and asks you. That’s next: resolving merge conflicts.

Check yourself

  1. 01When can Git fast-forward a merge?

    When the current branch is an ancestor of the branch being merged, meaning it has no commits of its own since they split. Git just moves the pointer.

  2. 02What are the three versions in a three-way merge?

    The common ancestor (merge base), the tip of your branch, and the tip of the branch being merged. Git applies both sides' changes relative to the ancestor.

  3. 03How many parents does a merge commit have, and why does it matter?

    Two. That records that both lines of history were combined, so git log --graph can draw the join and git log --first-parent can follow only the main line.

  4. 04You run git merge --ff-only and Git says it is not possible to fast-forward. What does that tell you?

    Both branches have commits the other lacks, so a merge commit or a rebase is needed. --ff-only refuses rather than create one.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark