What is a branch?

Topic 1 of 4 in Branching and merging6 min readBeginnerFree course

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.

A branch is just a movable pointer. Step 1, git switch -c feature: main and feature both point to commit c9 — nothing is copied. Step 2, commit on feature: a new commit e4 is created with c9 as its parent, and only the checked-out branch, feature, moves to it; main still points to c9. A branch is just a name for a commit ID, which is why branches are instant. Creating, switching and deleting branches is cheap, so use one for every piece of work.

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:

  1. Your feature work is safe on feature/checkout-redesign.
  2. You switch to main, create fix/price-rounding, fix the bug, and ship it.
  3. You switch back to your feature and carry on exactly where you left off.

Branches also give you:

  • A stable main branch. main only 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:

Terminal
git log --oneline --graph --all
Text
* 7c1d2e3 (HEAD -> main) Fix typo on main
| * 4b5a6f7 (feature) Add fast mode
|/
* 9e8f7a6 Second
* 1a2b3c4 First

Read 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.

Exercise

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?

Terminal
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 graph

Summary

  • A branch is a movable name for a commit; creating one copies nothing and takes milliseconds.
  • The current branch is the one HEAD points to; only it moves when you commit.
  • Branches let you work on several things in parallel and keep main stable.
  • 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.