[!TIP] Pro Tip: Forget the idea that a branch is a “copy” of your code. In Git, a branch is just a sticky note on a specific commit.
The Illusion of Heavy Branches
In many older version control systems (like SVN), creating a branch meant copying the entire source code directory. If your project was 1GB, creating a branch took minutes and consumed another 1GB of disk space.
In Git, creating a branch takes 0.01 seconds and consumes 41 bytes.
How? Because a branch in Git is nothing more than a simple text file containing a 40-character SHA-1 hash (plus a newline). It points to a commit. That’s it.
The 41-Byte Proof
Don’t believe me? Let’s look at the raw data.
$ git checkout -b feature-login
Switched to a new branch 'feature-login'
$ ls .git/refs/heads/
feature-login
main
$ cat .git/refs/heads/feature-login
e0c9047913364f3d2426830d1704285810795493
That hash e0c90... is simply the ID of the commit you are currently on. When you make a new commit, Git simply updates this file with the new commit hash.
Interactive: The Branch Pointer Simulator
Visualise how branches (green) and HEAD (red) move as you work.
HEAD: The “You Are Here” Marker
You might have seen HEAD in your logs. Think of HEAD as the “You Are Here” dot on a mall map.
- HEAD usually points to a Branch name. (e.g., “I am on
main”) - The Branch name points to a Commit. (e.g., “
mainis ate0c90”)
When you run git checkout feature-login, Git simply:
- Looks up
.git/refs/heads/feature-loginto find the commit hash. - Updates the files in your working directory to match that commit.
- Updates
.git/HEADto sayref: refs/heads/feature-login.
Essential Commands
1. Creating & Switching
The modern way (Git 2.23+):
# Create and switch in one go
git switch -c new-feature
# Just switch
git switch main
The classic way:
# Create and switch
git checkout -b new-feature
# Just switch
git checkout main
[!NOTE] Why the change?
git checkoutwas doing too many things (checking out files and switching branches).git switchis safer and more intuitive.
2. Listing Branches
# List local branches
git branch
# List all (local + remote)
git branch -a
# See the last commit on each branch
git branch -v
3. Deleting Branches
Since a branch is just a pointer, “deleting” it just removes the reference file. The commits still exist (until garbage collection runs).
# Safe delete (checks if merged)
git branch -d feature-login
# Force delete (if you want to discard work)
git branch -D feature-login
Common Gotcha: Detached HEAD
If you checkout a specific commit hash instead of a branch name, you enter “Detached HEAD” state.
git checkout e0c9047
What happened? HEAD is now pointing directly at a commit, not at a branch name. If you make new commits here, no branch name will track them. If you switch away, the garbage collector will eventually eat those commits.
Fix: Create a branch to save your spot.
git switch -c my-saved-work
Summary
- Branches are pointers, not copies.
- HEAD is a pointer to the current branch.
- Switching updates HEAD and your files.
- Use
git switchfor clarity overgit checkout.