git merge — merge commits

Watch git merge create a two-parent merge commit on an animated commit graph. See exactly when fast-forward is impossible because branches diverged.

Scenario

Speed
c1initc2base featurec3feat Ac4feat Bc5hotfixc6Merge branch 'f…HEADmainfeature

Branch state

mainc6◀ HEAD
featurec4

Current step 9/9

$ git merge feature

The histories diverged so fast-forward is impossible — a merge commit with two parents is created. (If main hadn't moved, the pointer would just slide forward — that's FF.)

$ git log --oneline --all

c6 (HEAD -> main) Merge branch 'feature'

c5 hotfix

c4 (feature) feat B

c3 feat A

c2 base feature

c1 init

File content (at HEAD) — app.js @ main

1initc1
2basec2
3featAc3
4featBc4
5hotfixc5
commit abandoned commitHEAD HEAD

Circles are commits; dimmed circles were abandoned by rebase/reset. A prime (c3') means same content, rewritten hash. In sandbox mode, run commands yourself with the buttons.

git merge joins two diverged lines of history back together. If your branch hasn't moved since the split, Git just slides the pointer forward — that's a fast-forward, and no new commit appears. But when both sides have new commits, Git creates a merge commit with two parents.

The two parents are the key: the first is your branch's previous tip, the second is the merged branch's tip. The Y-shape you see in git log --graph exists because of them. When there's no conflict, the merge commit is a snapshot containing both sides' changes.

Watch the scenario and compare what happens before and after main gets its own commit. Had main stayed put, the pointer would simply have slid to feature's tip. Because main moved, Git needs a real merge commit to join the histories. Git Branch Simulator

Frequently asked questions

What's the difference between fast-forward and a merge commit?
If your branch is an ancestor of the target, Git fast-forwards the pointer — no new commit. When both sides moved, a two-parent merge commit is created. Use --no-ff to force a merge commit even when FF is possible.
Why do merge conflicts happen?
When both branches changed the same lines differently. Git can't choose, so it inserts conflict markers (<<<<<<<) and lets you resolve, then you add and commit to finish the merge.
Should I merge or rebase?
Merge preserves history as it happened; rebase gives a linear history. Use merge on shared branches, and rebase to tidy local branches you haven't pushed yet.

Other scenarios

Related tools