Interview Prep/Git & GitHub/Git internals: objects, refs and the index

What exactly is a branch, and what does creating one cost?

IntermediateConceptGit internals: objects, refs and the index
Answer

A branch is a file containing a 40-hex commit ID, stored at .git/refs/heads/<name> (or in packed-refs). Creating one writes 41 bytes, so it is effectively free. Committing on a branch just moves that pointer forward to the new commit.

cat .git/refs/heads/main
git rev-parse main
All Git & GitHub questions Roadmap: Git & GitHub

More on Git internals: objects, refs and the index