Merge Commits

What

A Merge Commit is a unique type of commit that is generated when Git combines two diverging branches. Unlike a standard commit, which only points to a single parent commit, a merge commit points to two parent commits (one from each of the two merged branches).

How

When you perform a merge that requires combining diverging timelines, Git performs a three-way merge:

# Diverged branches before merging:
A - B - C    main
   \
    D - E    feature_branch

# After running 'git merge feature_branch' while on 'main':
A - B - C - F    main
   \     /
    D - E        feature_branch

During this merge, Git:

  1. Locates the best common ancestor (the “merge base”) where the two branches originally split (commit A).
  2. Compares the changes made on both branches since that common ancestor.
  3. Automatically combines the changes and records the result as a new merge commit F.
  4. Sets the parents of F to be both C and E.

Why

Because developer timelines split frequently, simple linear histories are rare. Merge commits act as historical anchors. They preserve the exact historical context, indicating exactly when a branch was merged, which commits were imported, and how conflicts (if any) were resolved, creating an auditable history of the codebase.

Summary

Diverged branches are merged via a three-way merge, producing a unique merge commit that references two separate parent commits to unify their histories.