Lesson Completion
Back to course

What is a Branch?

Beginner
10 minutes4.8Git

The Hook (The "Byte-Sized" Intro)

A Git branch costs 41 bytes. That's it — 41 bytes to create an entire parallel universe where you can experiment, break things, and build features without touching a single line of stable code. Branches are the reason Git makes collaboration possible. Without them, every developer would be stepping on every other developer's toes.

📖 What is a Branch?

A branch is a lightweight, movable pointer to a commit. It's not a copy of your project — it's just a label that says "I'm currently here." When you make a new commit on a branch, the pointer automatically moves forward to the new commit.

Conceptual Clarity

  • A branch is literally a 40-character file inside .git/refs/heads/ containing a commit hash
  • Creating a branch does not copy files — it just creates a new pointer
  • main is not special — it's just the default branch name. It follows the same rules as any other branch
  • HEAD is a special pointer that tracks which branch you're currently on
  • When you commit, the current branch pointer moves forward — HEAD follows automatically

How it works internally:

.git/refs/heads/main → a1b2c3d (commit hash) .git/refs/heads/feature → e4f5g6h (commit hash) .git/HEAD → ref: refs/heads/main (current branch)

Real-Life Analogy

Branches are like parallel timelines in a sci-fi movie. The main timeline (main) keeps running. You fork a new timeline (feature) to try something risky. If the experiment works, you merge the timelines. If it fails, you abandon the alternate timeline — the main one is untouched.

Visual Architecture

gitGraph commit id: "A" commit id: "B" branch feature/login checkout feature/login commit id: "C" commit id: "D" checkout main commit id: "E" merge feature/login id: "F"

Why It Matters

  • Isolation: Work on a feature without breaking main for your entire team
  • Parallel development: 10 developers can work on 10 features simultaneously
  • Risk-free experimentation: Create a branch, try something wild, delete it if it fails
  • Code review: Feature branches enable pull requests — the foundation of modern code review

Code

bash
# ─── List all local branches ─── git branch # Output: # feature/login # * main ← asterisk = current branch # ─── See where each branch points ─── git branch -v # Output: # feature/login e4f5g6h Add login form # * main a1b2c3d Update README # ─── List remote branches too ─── git branch -a # Output: # feature/login # * main # remotes/origin/main # remotes/origin/feature/login # ─── See the raw pointer (proof it's just a file) ─── cat .git/refs/heads/main # Output: a1b2c3de4f5g6h7i8j9k0l1m2n3o4p5q6r7s8t9 # ─── See what HEAD points to ─── cat .git/HEAD # Output: ref: refs/heads/main

Branch vs Copy

AspectGit BranchCopy/Clone
Storage41 bytes (pointer)Full project copy
SpeedInstantSlow (proportional to project size)
HistoryShared history, diverges from branch pointCompletely separate
Merging backBuilt-in merge toolsManual file comparison

Key Takeaways

  • A branch is a 41-byte pointer to a commit — not a copy of your project.
  • main is just a default branch name. It follows the same rules as all other branches.
  • HEAD points to the branch you're currently on.
  • Branches make parallel development, experimentation, and code review possible.

Interview Prep

  • Q: What is a Git branch internally? A: A branch is a lightweight, movable pointer stored as a file in .git/refs/heads/. The file contains the 40-character SHA-1 hash of the commit it points to. Creating a branch is just creating this file — no files are copied.

  • Q: What is HEAD in Git? A: HEAD is a symbolic reference that points to the current branch (e.g., ref: refs/heads/main). When you commit, the branch HEAD points to moves forward. In "detached HEAD" state, HEAD points directly to a commit instead of a branch.

  • Q: Why is branching in Git faster than in SVN? A: In SVN, creating a branch copies the entire directory tree. In Git, a branch is just a 41-byte pointer file — no files are copied, making it instantaneous regardless of project size.

Topics Covered

Git BranchingGit Fundamentals

Tags

#git#branch#pointer#beginner-friendly

Last Updated

2026-02-12