What is a branch?
Overview
Branches are Git’s superpower. They let you develop a feature, fix a bug or try an experiment in isolation, without disturbing the main line of work — and then combine the results. Many version control systems make branches slow and awkward. In Git they’re almost free, so professional teams use one for every task.
A branch is a pointer
You learned in HEAD and refs that a branch is a name pointing to a commit. That’s the whole implementation: when you create a branch, Git writes a new name that points to the current commit. No files are copied.
When you commit, the branch HEAD points to moves forward. Other branches stay where they are. That’s how two branches can start from the same commit and then diverge.
Why use branches?
Imagine you’re halfway through a large feature when an urgent bug is reported in production. Without branches, your half-finished code is mixed into the files you’d need to fix and release. With branches:
- Your feature work is safe on
feature/checkout-redesign. - You switch to
main, createfix/price-rounding, fix the bug, and ship it. - You switch back to your feature and carry on exactly where you left off.
Branches also give you:
- A stable main branch.
mainonly receives finished, reviewed work, so it’s always releasable. - Safe experiments. Try an idea on a branch; if it doesn’t work, delete the branch.
- Code review. On GitHub, a pull request is a request to merge one branch into another.
- Parallel teamwork. Everyone works on their own branches and integrates through
main.
The commit graph
As soon as two branches have their own commits, history is no longer a straight line — it’s a graph:
git log --oneline --graph --all* 7c1d2e3 (HEAD -> main) Fix typo on main
| * 4b5a6f7 (feature) Add fast mode
|/
* 9e8f7a6 Second
* 1a2b3c4 FirstRead it from the bottom up: First and Second are shared, then the lines split. main and feature each have one commit the other doesn’t. Eventually you’ll merge them back together (Merging branches).
Commits are reachable, not owned
People say “the commit is on the feature branch”, but strictly, a commit is on every branch it can be reached from by following parent links backwards. Above, First and Second are on both branches. Once feature is merged into main, its commits are reachable from main too — they’re “on main” as well.
That’s why deleting a merged branch loses nothing: the commits stay reachable from main. Deleting an unmerged branch is different — its unique commits become unreachable, which is why Git asks you to confirm (Creating and switching branches).
main is just a branch
There’s nothing special about main in Git itself. It’s the branch git init creates, and by convention it’s the one everyone treats as the official line of development. GitHub calls it the default branch — the one shown when you visit the repository and the target of new pull requests. Some projects use develop, trunk or (in older repositories) master instead.
How many branches?
Teams usually have:
- one long-lived branch,
main— sometimes plus release branches; - many short-lived branches, one per task, each living a few hours to a few days and deleted after merging.
Short-lived branches are the secret to painless merging: the less time a branch spends away from main, the less there is to reconcile. GitHub flow in chapter 9 builds a whole team workflow on this idea.
Think of it this way: commits are permanent points in history; branches are sticky notes you move around to mark where each line of work has got to.
Watch branches move
Create a repository with two commits on main. Create a branch feature without switching to it and confirm both branches point at the same commit. Switch to feature, make one commit, and show that only feature moved. Then switch back to main, make a different commit, and draw the resulting graph with git log --oneline --graph --all. How many commits are reachable from each branch?
mkdir branches && cd branches && git init -b main
echo "v1" > app.txt && git add . && git commit -qm "First"
echo "v2" > app.txt && git commit -qam "Second"
# 1. Create feature without switching; compare where both point
# 2. Commit on feature and compare again
# 3. Commit on main, then draw the graphmkdir branches && cd branches && git init -b main
echo "v1" > app.txt && git add . && git commit -qm "First"
echo "v2" > app.txt && git commit -qam "Second"
# 1. Create feature without switching; compare where both point
git branch feature
git branch -v # both at "Second"; * marks the current branch (main)
# 2. Commit on feature and compare again
git switch feature
echo "fast mode" > feature.txt && git add . && git commit -qm "Add fast mode"
git branch -v # feature moved, main didn't
# 3. Commit on main, then draw the graph
git switch main
echo "fix" > fix.txt && git add . && git commit -qm "Fix typo on main"
git log --oneline --graph --all
git rev-list --count main # 3
git rev-list --count feature # 3 (First, Second, Add fast mode)Summary
- A branch is a movable name for a commit; creating one copies nothing and takes milliseconds.
- The current branch is the one
HEADpoints to; only it moves when you commit. - Branches let you work on several things in parallel and keep
mainstable. - When two branches both get new commits, history forks — the commit graph shows both lines.
- Commits belong to every branch they’re reachable from, not to one branch.