Version control · intermediate

Git branches and merges

A Git branch is a movable name pointing to a commit; merging integrates histories by fast-forwarding when one tip is an ancestor or by creating a combined result from divergent parents.

Why it matters

The pointer-and-graph model makes branch creation cheap and explains conflicts, merge commits, shared history, and the consequences of rewriting published branches.

Mental model

How to reason about git branches and merges

Commits form a directed parent graph and branch names point at tips. New commits advance the current branch; merge finds common ancestry and combines changes made along the diverged paths.

Analogy

Two editors branch from the same manuscript edition and create separate edition chains. A merge reconciles their changes into an edition that acknowledges both lines of work.

Examples

See the boundary, not just the happy path

Worked example · Fast-forward

main A--B; feature A--B--C; merge feature into main

Because main's tip is an ancestor of C, Git can move the main pointer to C without creating a merge commit.

Worked example · Three-way merge

main adds C; feature adds D from common base B

Git compares each side with B and creates a result with both tips as parents when the histories diverged.

Useful contrast · Conflict is not a Git failure

both sides edit the same logical region differently

Git lacks enough intent to choose a result. A person must produce, test, and stage the resolved content.

Common mistakes

Misconceptions to remove early

Treating a branch as a copied directory

The branch is primarily a reference to a commit. Git reconstructs the selected tree and shares existing commit objects across branches.

Resolving conflicts by deleting markers only

A syntactically clean file can still combine behavior incorrectly. Understand both changes, run relevant tests, and inspect the staged resolution.

Quick check

Can you predict the result?

1. When can Git perform a fast-forward merge?
  • When the current branch tip is an ancestor of the branch being merged
  • Whenever both branches have the same name
  • Only when no files exist
Answer: When the current branch tip is an ancestor of the branch being merged
2. What information does a normal merge commit preserve?
Answer: It records the integrated tree and references both previously divergent parent tips.

Keep building

Authoritative references

Make the idea retrievable.

Study this concept with spaced repetition, next to the commands where you use it.

Get Terminaster