skipnothing/Git

Refs & HEAD

You run git switch dev and the files in your project change at once, yet not a single saved commit has moved. Something tiny on disk got rewritten, and finding it explains why branching costs almost nothing.

  • ▸A branch is a 41-byte file: one commit hash and a newline. That is the whole thing.
  • ▸Switching branches changes your files but moves no commit; only the tiny pointer marking your place is rewritten.
  • ▸The scary detached warning only means that file holds a bare hash instead of a branch name.
BUILDS ON
01

A branch is one line on disk

Switch to another branch and your files rearrange in an instant. Nothing about your saved history moved, though, so what did the command actually touch? Open the folder git keeps beside your code, in .git/, and look at the file that stands in for your current branch. It holds one line: a 40-character string like 007e5b8a5a33bf8646253f47c29dd1d9d408091c, and nothing else. That string is the name of a commit. A branch, the thing you pictured as a container holding all your work, is a single line pointing at one commit.

One caveat before you go looking: after git gc or git pack-refs (which git can run on its own), git may fold these loose files into a single .git/packed-refs file. If cat .git/refs/heads/main then reports the file missing, the branch is not gone, it moved into packed-refs, still one line, just relocated. git rev-parse main prints the hash either way.

OPEN THE BRANCH FILES

Keep going, sign up to unlock the rest

3 more parts in this topic, plus 18+ more topics in Git.

Sign up, it's freeSee the full Git