Git Three-Way Merge: Combine Divergent History
Three snapshots, one result
In the diverged lab, A is the common ancestor. main has B with main.txt and feature has C with feature.txt. Git uses a merge base and both branch tips to combine changes. A merge base is a best common ancestor; more complex graphs can have multiple merge bases. In this example there is one.
The merge base is A. Conceptual parent relations: B → A and C → A. Neither B nor C is an ancestor of the other.
Integrate both branches
Check that the working tree is clean and main is current. For a normal two-branch merge with compatible changes, Git can construct a combined snapshot automatically. --no-ff explicitly requests a merge commit when a merge is needed, even if a fast-forward would otherwise be possible.
git status --shortgit branch --show-currentgit merge --no-ff -m "Merge feature" featurecat main.txt feature.txtThe fixture prints Main work and Feature work on separate lines. The merge commit M has B as first parent and C as second parent; feature still points to C.
Verify the graph
Inspect the merge commit parents and resulting snapshot. A successful textual merge is not a guarantee of correct application behavior: run the project’s tests as well.
git rev-list --parents -n 1 HEADgit log --graph --oneline --allgit status --shortThe first line has three IDs: M, B, C. Short status is empty. A merge does not delete the source branch.
Practice
Draw A, B, C and M with arrows toward parents. Explain why checking only the file contents cannot prove that the merge commit has two parents.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Git Three-Way Merge: Combine Divergent History — knowledge check
Reason about merge history, conflict state and safe integration.
B and C each have parent A. What is the merge base?
A normal merge of divergent B and C succeeds. What are M’s parents?
What does --no-ff request when integration is needed?
What happens to the source branch after a normal merge?
Why run application tests after a clean merge?