How does a Git branch work internally and why is branching cheap in Git?
Discover how a Git branch is just a movable pointer to a commit, how HEAD and refs work, and why branching in Git is instant and inexpensive.
Expected Interview Answer
A Git branch is simply a lightweight, movable pointer to a single commit — internally just a small file containing a 40-character commit hash — so creating one costs almost nothing and requires no copying of files.
Each commit already stores a snapshot of the whole project plus a link to its parent commit, forming a chain of history. A branch is a named reference (stored under .git/refs/heads/) that points to the tip of one such chain, and HEAD points to the current branch. When you commit, Git creates the new commit and just advances the branch pointer forward. Because branching only writes a tiny reference file rather than duplicating the codebase, it is instant and inexpensive compared to older version control systems that copied entire directories.
- Branch creation is near-instant regardless of repository size
- No file duplication — only a small ref file is written
- Encourages frequent, isolated feature and experiment branches
- Switching branches just moves HEAD to another pointer
- Merging is efficient because history is a graph of commits
AI Mentor Explanation
A branch is like a bookmark clipped onto the exact ball in a long scorebook where a particular innings stands. Starting a new innings does not photocopy the entire book — you just clip in a fresh bookmark at the current ball. Each delivery you record simply slides that bookmark forward, so tracking many innings costs only a few cheap clips, not new books.
Step-by-Step Explanation
Step 1
Understand commits
Each commit stores a full snapshot plus a pointer to its parent, forming a chain of history.
Step 2
See what a branch is
A branch is a file under .git/refs/heads/ containing the 40-char hash of one commit.
Step 3
Know HEAD's role
HEAD is a pointer to the current branch, telling Git which branch tip you are on.
Step 4
Create a branch
git branch <name> writes one small ref file — no file copying, so it is instant.
Step 5
Commit and advance
On commit, Git creates the commit object and moves the branch pointer forward to it.
What Interviewer Expects
- Knowing a branch is just a movable pointer to a commit
- Understanding commits chain via parent pointers
- Explaining that refs live under .git/refs/heads/
- Grasping HEAD as a pointer to the current branch
- Articulating why no file copying makes branching cheap
Common Mistakes
- Thinking a branch copies the entire codebase or working directory
- Believing branches store their own separate file contents
- Confusing HEAD with a branch name rather than a pointer to one
- Not knowing branches are tiny files in .git/refs/heads/
- Assuming branching is slow on large repositories
Best Answer (HR Friendly)
“A Git branch is just a small bookmark pointing to a specific save point in the project's history, not a copy of all the files. Because creating a branch only writes a tiny reference, branching is instant no matter how big the project is.”
Code Example
# Create a new branch (writes one tiny ref file)
git branch feature-x
# A branch ref is literally a file holding a commit hash
cat .git/refs/heads/feature-x
# -> 9f2a4c1e8b7d3a5f6c0e1d2b3a4c5d6e7f8a9b0c
# HEAD points to the current branch, not a commit directly
cat .git/HEAD
# -> ref: refs/heads/main
# Switch branches: Git just moves HEAD (no file copying)
git switch feature-x
# Commit advances the branch pointer forward to the new commit
git commit -am "Add feature X"Follow-up Questions
- What exactly does HEAD point to and how does a detached HEAD occur?
- How does a commit reference its parent commit?
- What is the difference between a branch and a tag internally?
- How does fast-forward merging relate to moving branch pointers?
- Where are remote-tracking branch refs stored?
MCQ Practice
1. What does a Git branch physically consist of?
A branch is just a ref file under .git/refs/heads/ holding the 40-character hash of the commit it points to.
2. Why is creating a branch cheap in Git?
Branch creation writes one small pointer file rather than duplicating any project files, so it is near-instant regardless of repo size.
3. What does HEAD normally point to?
HEAD is usually a symbolic ref pointing to the current branch (e.g. ref: refs/heads/main), which in turn points to a commit.
Flash Cards
What is a Git branch internally? — A movable pointer — a small file under .git/refs/heads/ holding a single commit hash.
Why is branching cheap? — It only writes a tiny reference file; no project files are copied, so it's instant at any repo size.
What does HEAD point to? — The current branch (a symbolic ref like refs/heads/main), which points to the tip commit.
What happens to the branch on commit? — Git creates the new commit and advances the branch pointer forward to it.
How do commits form history? — Each commit stores a snapshot plus a pointer to its parent, chaining commits into a graph.