AI Hints
Git & GitHub · Lesson 108 of 144

What a Branch Really Is

A file with a hash.

⏱ 2 min read▶ Video at 1:58:31🎓 Free
Watch this lesson in the Git & GitHub courseWatch on YouTube ↗Full Git & GitHub course

Here's the most surprising fact about Git. A branch is just a tiny text file, holding one commit hash. That's why branches are so cheap.

What a Branch Really Is
From the video: What a Branch Really Is
  • A branch is a tiny file
  • Holding one commit hash
  • That's why they're cheap

Branches are files

Inside dot git, refs, heads, there's one file per branch. Cat main shows a single hash, our latest commit. And the file called head simply says, ref, refs slash heads slash main. That's how Git knows which branch you're on.

git
ls .git/refs/heads
dark
main
cat .git/refs/heads/main
f63a17af33cd97f30d014023639480f94ef09578
cat .git/HEAD
ref: refs/heads/main

Everything makes sense now

Now everything fits together. Creating a branch writes one small file. Committing writes new objects, and moves the branch file to the new hash. Switching branches changes the head file. And a tag is just like a branch that never moves.

git branchwrites a tiny file
git commitmoves the branch
git switchchanges HEAD
git taga label that stays

Summary

A branch is a file holding a hash, and head says which branch you're on. That's the whole trick. Next, publishing your site for free with GitHub.

Key takeaways
  • Branch = file with a hash
  • HEAD = which branch
  • Cheap and fast
Next up: GitHub Pages — a free website