CodeBlanks Logo Icon CodeBlanks

Three-way merge

Learn the three-way merge commit.

Three-Way Merge

When two branches share a common history but both contain separate commits, Git can perform a three-way merge. Git combines changes by comparing the common ancestor commit with the latest commits of both branches.

       B -- C  (main)
      /
A ---
      \
       D -- E  (feature)

This operation generally creates a new merge commit that joins the two branches.

How Is a Merge Commit Created?

When a three-way merge completes, Git creates a merge commit that connects the histories of both branches. Unlike a regular commit, this commit has two parent commits.

       B -- C ---- M  (main)
      /          /
A ---          /
      \       /
       D -- E    (feature)

The M commit represents the merge result and preserves the history of both branches.

Practice Build a Merge Commit Command

While on the main branch, merge the feature branch by creating a separate merge commit instead of fast-forwarding. Assign the command to mergeCommand and print it.

Starter code

terminal
const mergeCommand = "";

// Birleştirme komutunu yazdır

Sample solution

terminal
const mergeCommand = "git merge --no-ff feature";

console.log(mergeCommand);

Expected output

git merge --no-ff feature

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which three commits does Git consider in a three-way merge?

  • The first commits of both branches and the latest commit
  • The three oldest commits
  • The common ancestor and the latest commits of both branches Correct answer
  • Only the three latest commits of the current branch

Explanation: A three-way merge uses the common ancestor commit and the latest commit from each branch.

2 The Merge Commit Option

Question
git switch main
git merge ____
Answer
git switch main
git merge --no-ff feature

Explanation: The --no-ff option makes Git create a separate merge commit even when a fast-forward would be possible.

3 Three-Way Merge Components

Term Meaning
Ortak ata The shared branch history
İki dalın sonu The compared tip commits
Merge commit A commit with two parents
Üç yönlü işlem Compares three commits

All lessons in Beginner