tiny-git merge <target-branch> combines divergent development histories into a unified snapshot.
When merging target branch C into current HEAD branch B, tiny-git must first locate their best common ancestor A:
B (current branch / HEAD)
/ \
A C (target branch)
tiny-gitperforms a Breadth-First Search (BFS) starting at commitB, recording all ancestors and their topological depth.- It then performs a BFS starting at commit
C, searching for the first commit that is also present inB's ancestor set. - This commit is chosen as the Lowest Common Ancestor (
A).
If A == B (the current branch is an ancestor of the target branch), no divergent history exists:
A (HEAD / main) ───► C (feature)
In this case, tiny-git performs a Fast-Forward Merge:
- Working tree files and index entries are updated to match snapshot
C. - The current branch reference (
refs/heads/main) is advanced directly toC. - No merge commit is created.
When B and C have diverged from A, tiny-git performs a true three-way merge across all files:
Ancestor (A) |
HEAD (B) |
Target (C) |
Result | Explanation |
|---|---|---|---|---|
val |
val |
val_C |
val_C |
Only Target changed; take Target's change |
val |
val_B |
val |
val_B |
Only HEAD changed; take HEAD's change |
val |
val_B |
val_B |
val_B |
Both made identical changes; clean merge |
val |
val_B |
val_C |
3-Way Text | Both changed differently; perform line merge |
For text files where both B and C made changes:
tiny-gitcomputes edit regions fromA → BandA → C.- If
BandCmodified non-overlapping lines ofA, the changes are combined automatically without conflict. - If
BandCmodified overlapping lines ofAwith different content,tiny-gitinserts conflict markers into the working directory file:<<<<<<< HEAD Current HEAD lines... ======= Incoming feature branch lines... >>>>>>> feature
When a three-way merge succeeds without conflicts, tiny-git creates a Merge Commit:
- It has two parents:
[HEAD_commit, target_commit]. - Its commit message defaults to
Merge branch '<target>'. - It records the combined snapshot tree in
.tinygit/objects/.