Course contentsShow
← Software Engineering
Lesson 59 of 1,9332. Version Control with GitFree lesson

Three-Way Merges

Learn how Git performs three-way merges when branches have diverged, creating a new merge commit with two parents.

Three-Way Merges

You'll learn how Git creates a merge commit when two branches have diverged with different changes.

When Fast-Forward Isn't Possible

In the previous lesson, you learned about fast-forward merges where Git simply moves the branch pointer forward. But what happens when both branches have new commits since they diverged? That's when Git performs a three-way merge.

The Three Points

A three-way merge gets its name because Git looks at three snapshots:

  1. The common ancestor — the commit where the branches originally split
  2. Your current branch's tip — the latest commit on the branch you're merging into
  3. The other branch's tip — the latest commit on the branch you're merging from

Think of it like two authors editing different chapters of the same book. They both started from the same manuscript (common ancestor), each made their own edits (branch tips), and now you need to combine both sets of changes into one final version.

Creating a Merge Commit

Git automatically combines the changes from both branches and creates a new commit called a merge commit. This special commit has two parents instead of one — it points back to both branch tips, preserving the complete history of both lines of development.

When you merge and Git performs a three-way merge, it opens your editor asking for a commit message (usually defaulting to something like "Merge branch 'feature' into main").

Key Takeaway: A three-way merge happens when branches have diverged with different commits, and Git creates a special merge commit with two parents to combine both histories.