CodeOath
← All posts
Git65 min total · 17 parts

Git Internals and Workflows: Branching, Merging, and Rebasing

Contents — Part 2 of 17: A Commit Is a Snapshot; a Branch Is Just a Pointer
Part 2 of 17 · ~1 min

A Commit Is a Snapshot; a Branch Is Just a Pointer

Every commit stores a full snapshot of your project's tracked files (not a diff, though Git compresses very efficiently under the hood by reusing unchanged file contents across commits) plus a pointer to its parent commit — or, for a merge commit, two parents. A branch is nothing more than a movable label pointing at one commit — creating a branch is instant precisely because it doesn't copy any files, it just writes a new pointer:

git branch feature      # create a pointer named "feature" at the current commit
git switch feature       # move HEAD to point at "feature" instead

HEAD itself is just a pointer to whichever branch (or, in detached-HEAD state, commit) you currently have checked out. When you commit, Git creates the new commit object, then moves whichever branch HEAD points to forward to it — this is the entire mechanism behind "committing to a branch." There's no special storage per branch; the commits themselves are shared, content-addressed objects that any number of branch pointers can reference simultaneously.