Branching & Merging

Branches are just pointers — understanding this makes branching instant and merge strategies clear

Why

Branches are the core of parallel team work. Because a branch is just a 41-byte pointer to a commit, creating or deleting one is instantaneous — there's no reason to fear branching often. Understanding what kind of merge git performs, and why, determines how your project history looks and how easy reverting or bisecting will be later.

Intuition

A branch is a file in .git/refs/heads/ containing one SHA. That's it. HEAD is another file — it points to the current branch name (or directly to a commit SHA in "detached HEAD" state). When you commit, git writes the new SHA into the current branch file.

Fast-forward merge: the target branch hasn't diverged — git just moves the pointer forward. Three-way merge: both branches have new commits — git finds the common ancestor, combines changes, and creates a merge commit with two parents.

Explanation
Branch operations
git switch -c feature/login # create + switch (Git 2.23+) git checkout -b feature/login # older equivalent git branch -d feature/login # delete (safe — warns if unmerged) git branch -D feature/login # force delete git log --oneline --graph --all # visualise all branches and HEAD
Fast-forward vs three-way merge
Fast-forward
No new commits on the target since branching. Git moves the pointer forward. No merge commit — linear history. Happens automatically when possible.
Three-way (recursive)
Both branches have diverged from a common ancestor. Git finds that ancestor, computes diffs to each tip, combines them, and creates a merge commit with two parents.
# Force a merge commit even when fast-forward is possible git merge --no-ff feature/login # Conflict resolution workflow git merge feature/login # conflict! # edit conflicted files, remove <<<<< ===== >>>>> markers git add resolved-file.py git commit # completes the merge