Part II: Chapter 2.5

References (refs)

What actually is a branch in Git?

People often talk about branches as parallel timelines or diverging paths, but under the hood, they are remarkably simple. A branch is not a copy of your files. A branch is just a Reference (or a "ref"). It is quite literally just a text file containing 40 characters: a commit hash.

refs/heads/main

Every time you create a new commit, Git automatically updates this little text file to point to the newest commit's hash.

That's it. That's the entire magic behind branching. When you switch to a different branch, Git just looks at the commit hash written in that branch's text file, and updates your working directory to match it.

.git/refs/heads/main
9f8a
9f8aInitial commit
main

Proving the Architecture

Let's prove this by inspecting the actual contents of the `main` branch inside the `.git/` folder.

Terminal
$ls .git/refs/heads/
feature-login main
$cat .git/refs/heads/main
9f8a357851f9eafaa9430c5e7b2f6efb35785199