Topic 2.1
Branches: What They Actually Are
In one line
A branch is nothing more than a movable pointer to a single commit — not a copy of the project, not a separate folder — and that one fact is the key to everything else in this phase.
Think of it like this
A bookmark in a book, not a photocopy of the pages. Moving a bookmark doesn't duplicate anything — it just marks a different page as 'where you currently are.' A Git branch is exactly a bookmark pointing at one specific commit; creating a branch is essentially free and instant because it copies nothing.
Key ideas
- 01
Technically, a branch is a small file containing a single SHA — the commit it currently points to.
git branch <name>creates a new pointer at your CURRENT commit; it does not copy any files, and creating even hundreds of branches costs almost no disk space, since they're just pointers into the same shared commit history. - 02
git switch <branch>(the modern, clearer command, replacing the older overloadedcheckoutfor this purpose) changes which branch HEAD points to, and updates your working directory to match that branch's latest commit.git switch -c <new-branch>creates a new branch AND switches to it in one step — the single most common way branches actually get created in practice. - 03
When you make a new commit while on a branch, that branch's pointer automatically moves FORWARD to the new commit — this is the entire mechanism of 'working on a branch': every commit you make just advances that one pointer, while other branches' pointers stay wherever they were, untouched.
- 04
The default branch (commonly
main) is not technically special to Git itself — it's just a branch like any other, that happens to be the one your team has agreed to treat as the authoritative, deployable line of history. Nothing stops you from renaming it or treating a different branch as primary, though doing so against convention would genuinely confuse collaborators. - 05
git branch(no arguments) lists every LOCAL branch, with a*marking your current one.git branch -aincludes remote-tracking branches too (Phase 3).git branch -d <name>deletes a branch — safely refusing if it has unmerged commits — whilegit branch -Dforce-deletes regardless.
Code & diagrams
Three branches, three pointers, one shared commit history underneath.
Watch a branch pointer move as you commit — nothing else changes.
cd ~/scratch/my-first-repo
# See your current branch and every other local branch
git branch
# Create a new branch AND switch to it in one step
git switch -c feature/add-greeting
# Confirm you're now on it
git branch
# * feature/add-greeting
# main
# Commit while on this branch — only THIS branch's pointer moves
echo "Hello!" >> notes.txt
git add notes.txt
git commit -m "Add a greeting"
# Switch back — main is exactly where you left it, unaffected
git switch main
cat notes.txt
# does NOT include "Hello!" — that commit only exists on feature/add-greeting
git switch feature/add-greeting
cat notes.txt
# includes "Hello!" again — you're back on that branch's pointerExplain it without notes
Why is creating a new Git branch essentially instant and free, regardless of how large the project is?
You commit while on a feature branch, then switch to main. Why doesn't main show that new commit?
Practice
Create a new branch, make a commit on it, then switch back to main and confirm with cat or your editor that the change genuinely isn't there.
Run git branch -a in a repo and identify which branch is currently checked out (marked with *) versus every other branch that exists.
Trade-offs
- ↔
Because branches are so cheap to create, many teams encourage creating a new one for every single piece of work, however small — genuinely good practice for keeping unrelated work isolated, though a repository can accumulate a large number of stale, merged branches over time if nobody deletes them afterward (
git branch -d, or a hosting platform's own 'delete branch after merge' setting), which is purely a housekeeping cost, not a performance one.
Done when you can
I can explain why a branch is 'just a pointer,' not a copy of the project.
I can create, switch between, and list branches confidently.
I understand that committing only moves the pointer of whichever branch is currently checked out.