Yeswanth Madasu

Day 08/13 of Git 101 Series.

MERGE


Concept Explanation

Merging combines two branches into one.
If the target branch has not diverged, Git performs a fast-forward, simply moving the branch pointer ahead.
If the branches have diverged, Git creates a merge commit with two parents.
Fast-forward is pointer movement; a merge commit is an explicit node that joins two histories.


Commands

git checkout main
git merge feature # fast-forward if possible
git merge --no-ff feature # force creation of a merge commit
git merge --abort # stop a failing merge


Strong Example

Starting history:

main: A → B
feature: B → C → D

main has not moved since branching.
Running:

git checkout main
git merge feature

main simply jumps to D — no new commit.

If main had new work (A → B → E), then Git cannot fast-forward.
git merge feature would create a new merge commit M with parents E and D.
M records the exact point where two lines of development joined.


Real World Use Case

A team develops a feature inside a separate branch.
After testing, they merge it into main.
Fast-forward maintains a clean linear history.
Merge commits (--no-ff) preserve a visible integration point that marks the feature as a coherent unit, useful for auditing and debugging.


Underhood Explanation

Git performs a 3-way merge using:

  1. Merge base (common ancestor)
  2. HEAD (destination branch)
  3. Merge source (incoming branch)

Git computes differences base→HEAD and base→source.
If modifications are in separate regions, merge is automatic.
If both modify the same region differently, Git inserts conflict markers.

Merge commits store two parent IDs, which makes them special nodes in Git’s directed acyclic graph.