Skip to content

Merge

A merge combines the histories of two branches into one, producing a new commit with two parents when the histories diverged, or a fast-forward move when they did not.

Definition

What it is

When you merge branch A into branch B, Git finds the common ancestor C. If B is a direct descendant of A's tip, Git "fast-forwards" B to A's tip: no merge commit is needed, the history is linear. If A and B diverged (both have commits C does not have), Git performs a three-way merge: it combines the diffs A-C and B-C, creates a new commit M with two parents (A and B), and B's pointer moves to M.

A merge never rewrites history. Both A's and B's commits survive intact. That is the defining property of a merge and the reason it is safe: it does not touch what already happened.

Why it matters

A merge is the tool for combining divergent work without destroying either side. The trade-off is history shape: a merge creates a "bubble" in the graph (two parents join into one commit), which some teams find noisy. Rebase is the alternative when a linear history is preferred, at the cost of rewriting commits.

Merge vs rebase vs squash vs cherry-pick

Aspect

Merge

Rebase

Squash

Cherry-pick

History shape

Bubble

Linear

Linear, one commit

Duplicated commit

Rewrites history

No

Yes

Yes

No (new commit)

Preserves branches

Yes

No

No

N/A

Conflict resolution

Once at merge time

Per commit replayed

Once

Once per commit

Typical use

Feature → main

Feature onto latest main

Cleanup before merge

Port a fix

The three-way merge

graph TD
  C[Common ancestor C] --> A[Branch A tip]
  C --> B[Branch B tip]
  A --> M[Merge commit M]
  B --> M
  style C fill:#FFF3E0
  style M fill:#E8F5E9

How to merge without breaking

Merging without pain

  1. Merge small branches

    A 20-commit branch conflicts more than a 2-commit branch. Merge daily, not monthly.

  2. Resolve conflicts locally

    Read every conflict mark; do not blindly accept "theirs". A wrong conflict resolution is a silent bug.

  3. Use `--no-ff` for feature branches

    A merge commit records the merge in history. git merge --no-ff feature/x makes it visible.

  4. Write the merge message

    Default is "Merge branch 'x' into 'y'". If the branch has 20 commits, list them in the body. Future-you will thank you.

Common failures

Origin

The word comes from Latin mergere, "to dive, to plunge, to immerse". English used it in the 17th century for combining streams and rivers. Version control adopted it in the 1980s for combining two divergent histories of the same file. Git in 2005 formalized the three-way merge as the default: find the ancestor, combine the diffs, create the merge commit with two parents.

Merging is the operation of bringing two or more branches together. The result is a new commit with multiple parents, or a fast-forward move if one branch is a direct descendant of the other.

— Chacon & Straub, *Pro Git*, 2014 (paraphrase)
1980s · merge en RCS/CVS
2005 · Git three-way merge por defecto
2008 · GitHub introduce Pull Requests
2010 · `--no-ff` adoptado en flujos Git Flow
2 · padres del merge commit

Evidence

FAQ

How are conflicts computed?

Three-way: compare A, B and C. Conflicts appear only where A and B both changed the same line relative to C.

When to use `--no-ff`?

For feature branches, always. It preserves the merge in history as a distinct event.

What is a fast-forward?

When B has not moved since A branched, merging A into B just moves B's pointer. No merge commit.

Merge vs rebase?

Merge preserves both histories, safe but noisy. Rebase rewrites one side onto the other, clean but destructive.

Connections

Etymology

Origin: borrowed

  • merge (verb): from Latin mergere, "to dive, to plunge"

Semantic network

MergeGitversion-coBranchRebaseCommitStashfast-forwa

● broader ● narrower ● related

Broader

  • Git
  • version-control

Related

Used in the garden

Sources