Skip to content

๐ŸŒฟ 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!

Git Branches Introduction

Branches as Pointers


๐Ÿ“บ Video Reference โ€‹

ResourceLink
๐ŸŽฌ VideoGit Branches
๐Ÿ“„ Transcript03-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 โ€‹

bash
# 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 = A special pointer that tells Git which branch (or commit) you're currently on.

HEAD Points to a Branch โ€‹

What's Inside HEAD? โ€‹

HEAD Explained

bash
# When on a branch:
cat .git/HEAD
# Output: ref: refs/heads/main

# HEAD contains a REFERENCE to a branch, not a commit SHA directly

Detached HEAD State โ€‹

Sometimes HEAD points directly to a commit (not a branch):

bash
# 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:

bash
# Git creates a new file:
echo "a1b2c3d4e5f6789012345678901234567890abcd" > .git/refs/heads/test

# That's ALL that happens! Just a new file with the same commit SHA

Switching Branches: git checkout test โ€‹

What Actually Happens:

bash
# Git changes HEAD to point to test:
echo "ref: refs/heads/test" > .git/HEAD

# Plus updates the working directory to match that commit's tree

Making a Commit on a Branch โ€‹

What Actually Happens:

  1. Git creates a new commit object
  2. Git updates the branch file with the new commit SHA:
    bash
    echo "b2c3d4e5f6789012345678901234567890abcdef" > .git/refs/heads/test

Switching Back: git checkout main โ€‹

Branch Operations

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 โ€‹

bash
# 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 -v

See HEAD โ€‹

bash
# What does HEAD contain?
cat .git/HEAD

# If on main branch:
# Output: ref: refs/heads/main

Create Branch Manually โ€‹

bash
# 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-branch

Switch Branch Manually โ€‹

bash
# 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 โ€‹

CommandWhat It Does Under the Hood
git branch testCreates .git/refs/heads/test with current commit SHA
git checkout testWrites ref: refs/heads/test to .git/HEAD
git checkout -b testBoth of the above
git commitCreates commit, updates current branch file with new SHA
git branch -d testDeletes .git/refs/heads/test file
git merge testCreates merge commit, updates current branch file

๐ŸŽฏ Key Takeaways โ€‹


๐ŸŽฏ Branches Summary โ€‹

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/main

It'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.

Continue to Lesson 04a โ†’


๐Ÿ“ Summary โ€‹

ConceptReality
BranchA file in .git/refs/heads/ containing a commit SHA
HEADA file (.git/HEAD) pointing to current branch or commit
Creating branchCreating a 41-byte file
Switching branchUpdating HEAD file
CommittingCreating commit object + updating branch file
Branches are just pointers. That's the secret! ๐ŸŽ‰

Released under the MIT License.