๐ฟ Git Branches - Named References to Commits โ
๐ฏ Branches Are Just Pointers!
A branch is nothing more than a file containing a 40-character commit SHA. That's it!


๐บ Video Reference โ
| Resource | Link |
|---|---|
| ๐ฌ Video | Git Branches |
| ๐ Transcript | 03-git-branches.txt |
๐ง What We Think Branches Are โ
"We usually think about branches as a sequence of changes related to each other"
We think of branches as separate lines of development...
๐ What Branches ACTUALLY Are โ
"Under the hood, a branch is just a named reference to a commit"
The Reality โ
โ What Branches Are NOT
- Separate copies of code
- Folders or directories
- Complex data structures
- Heavy objects to create
โ What Branches ARE
- A simple text file
- Contains a 40-char SHA
- Located in
.git/refs/heads/ - ~41 bytes total!
๐ Branch Storage Location โ
.git/
โโโ HEAD โ Points to current branch
โโโ refs/
โ โโโ heads/
โ โ โโโ main โ File containing: a1b2c3d4e5f6...
โ โ โโโ feature โ File containing: 9x8y7z6w5v4u...
โ โ โโโ bugfix โ File containing: 1a2b3c4d5e6f...
โ โโโ tags/
โ โโโ v1.0 โ File containing: deadbeef1234...Let's Look Inside โ
# What's in the main branch file?
cat .git/refs/heads/main
# Output: a1b2c3d4e5f6789012345678901234567890abcd
# That's it! Just 40 characters (+ newline)
wc -c .git/refs/heads/main
# Output: 41 .git/refs/heads/main๐ Understanding HEAD โ
Definition โ
HEAD Points to a Branch โ
What's Inside HEAD? โ

# When on a branch:
cat .git/HEAD
# Output: ref: refs/heads/main
# HEAD contains a REFERENCE to a branch, not a commit SHA directlyDetached HEAD State โ
Sometimes HEAD points directly to a commit (not a branch):
# In detached HEAD state:
cat .git/HEAD
# Output: a1b2c3d4e5f6789012345678901234567890abcd
# Direct SHA instead of branch reference!๐ How Branch Operations Work โ
Creating a Branch: git branch test โ
What Actually Happens:
# Git creates a new file:
echo "a1b2c3d4e5f6789012345678901234567890abcd" > .git/refs/heads/test
# That's ALL that happens! Just a new file with the same commit SHASwitching Branches: git checkout test โ
What Actually Happens:
# Git changes HEAD to point to test:
echo "ref: refs/heads/test" > .git/HEAD
# Plus updates the working directory to match that commit's treeMaking a Commit on a Branch โ
What Actually Happens:
- Git creates a new commit object
- Git updates the branch file with the new commit SHA:bash
echo "b2c3d4e5f6789012345678901234567890abcdef" > .git/refs/heads/test
Switching Back: git checkout main โ

Now main and test point to different commits!
Committing on Main (Divergence) โ
Now we have diverged branches! Both main and test have commits that the other doesn't.
๐งช Hands-On Lab: Exploring Branches โ
See Branch as Files โ
# List all local branches
ls -la .git/refs/heads/
# See contents of main branch
cat .git/refs/heads/main
# Compare with git branch
git branch -vSee HEAD โ
# What does HEAD contain?
cat .git/HEAD
# If on main branch:
# Output: ref: refs/heads/mainCreate Branch Manually โ
# Get current commit SHA
git rev-parse HEAD
# Output: a1b2c3d4e5f6789012345678901234567890abcd
# Create branch manually (same as git branch manual-branch)
echo "a1b2c3d4e5f6789012345678901234567890abcd" > .git/refs/heads/manual-branch
# Verify it exists
git branch
# Output:
# main
# * manual-branchSwitch Branch Manually โ
# Switch to new branch (same as git checkout manual-branch)
echo "ref: refs/heads/manual-branch" > .git/HEAD
# Verify
git branch
# Shows manual-branch with asterisk๐ Branch Commands vs What Actually Happens โ
| Command | What It Does Under the Hood |
|---|---|
git branch test | Creates .git/refs/heads/test with current commit SHA |
git checkout test | Writes ref: refs/heads/test to .git/HEAD |
git checkout -b test | Both of the above |
git commit | Creates commit, updates current branch file with new SHA |
git branch -d test | Deletes .git/refs/heads/test file |
git merge test | Creates merge commit, updates current branch file |
๐ฏ Key Takeaways โ
๐ฏ Branches Summary โ

๐ง Quiz: Test Your Understanding โ
1. Where is the 'main' branch stored?
๐ .git/refs/heads/main
It's just a text file containing the SHA of the commit it points to.
2. What does HEAD contain when you're on the main branch?
ref: refs/heads/mainIt's a symbolic reference to the branch, not the commit SHA directly.
3. What happens when you create a new branch?
Git creates a new file in .git/refs/heads/ with the current commit's SHA. That's it!
4. Why are branches "cheap" in Git?
Because they're just 41-byte files! Creating a branch doesn't copy any code or create any large data structures.
๐ What's Next? โ
๐ง Next: Creating a Repository From Scratch (Lesson 04a)
Now that you understand objects and branches, let's put it all together! We'll create a complete Git repository without using git init, git add, or git commit.
๐ Summary โ
| Concept | Reality |
|---|---|
| Branch | A file in .git/refs/heads/ containing a commit SHA |
| HEAD | A file (.git/HEAD) pointing to current branch or commit |
| Creating branch | Creating a 41-byte file |
| Switching branch | Updating HEAD file |
| Committing | Creating commit object + updating branch file |