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 mainBecause 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 BGit 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 differentlyGit 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
2. What information does a normal merge commit preserve?
Keep building