What Branches Are and Why They Matter
Branching is the feature that makes Git genuinely powerful for real-world development. Without branches, everyone working on a project is always editing the same line of history — every change is immediately mixed with everyone else’s. With branches, each developer works in isolation, and changes are only combined when they are ready.
Understanding what a branch actually is in Git makes everything about branching easier to reason about.
What a branch is
Section titled “What a branch is”A branch is a lightweight movable pointer to a commit.
That is the whole thing. A branch is not a copy of your code. It is not a separate folder. It is a file containing a 40-character commit hash — a pointer to one specific commit in the repository.
When you create a commit on a branch, the branch pointer automatically moves forward to point at the new commit. Every new commit moves the pointer. The branch is always at the tip of the line of commits you made on it.
Initial commit → Second commit → Third commit ↑ main (branch pointer)When you create a new branch called feature, it starts as another pointer to the same commit:
Initial commit → Second commit → Third commit ↑ ↑ main featureBoth main and feature point at the same commit. Now if you make a commit on feature:
Initial commit → Second commit → Third commit → Feature commit ↑ ↑ main featuremain has not moved. feature has advanced. The two branches now have different histories from that split point — but they share all of the commits before it.
HEAD: your current position
Section titled “HEAD: your current position”HEAD is a special pointer that tells Git which branch (and therefore which commit) you are currently on. When you see HEAD -> main in git log output, it means HEAD is pointing at the main branch.
When you switch branches, HEAD moves:
git switch featureNow HEAD -> feature. Any new commits you make go onto feature and leave main unchanged.
Why branches matter
Section titled “Why branches matter”Without branches, you have no safe way to work on a new feature while the current version of the site needs a bug fix. If you start building a new navigation section but the contact form breaks halfway through, you are stuck — your unfinished navigation and the broken contact form are in the same place.
With branches:
mainalways contains working, deployable code- Feature work happens on a feature branch:
feature/new-navigation - A bug fix happens on its own branch:
fix/contact-form-validation - Each branch is merged into
mainonly when the work is complete and tested
This is the foundation of every professional Git workflow. Teams with one developer, teams with hundreds, and every open-source project in the world work this way.
The cost of a branch
Section titled “The cost of a branch”Creating a branch in Git is nearly free. It creates a single file — around 40 bytes — containing the commit hash it points to. There is no copying, no duplication of files, no performance overhead. This is why Git encourages branching for everything, including tiny changes.
Exercise
Section titled “Exercise”You do not need to create a branch yet — the next lesson covers that in detail. For now, build the mental model:
-
Run
git log --onelinein yourgit-practicefolder. Find the hash of your most recent commit. -
Run
git log --oneline --graph --all. Notice thatHEADandmainboth point to the same commit. There is currently only one branch. -
Read the output of
git statusand find the “On branch main” line. This is HEAD pointing tomain.
Think about what it means that main is just a pointer to that one commit. The history did not change — the pointer just moved forward each time you committed.
- A branch is a lightweight movable pointer to a commit — a file containing a commit hash.
- When you commit on a branch, the branch pointer automatically moves forward to the new commit.
HEADis a special pointer indicating your current position.HEAD -> mainmeans you are onmain.- Branches are nearly free to create — no code is copied, no files are duplicated.
- Branches allow parallel lines of development: feature work, bug fixes, and experiments that do not affect each other until you choose to merge them.
The next lesson covers creating and switching branches — the two commands you will use dozens of times in every project.