How Git branches work
BeginnerCommits that point to their parents, branches that are just pointers to commits, HEAD that points to a branch, and what happens when two branches are merged.
Empty
Empty
Spaceplay or pause←→previous or next stepRback to the start
Explanation
3 min read
On this page
What is a branch in Git?
A commit is a snapshot of your project at one moment, with a message, an author and a pointer
to the commit (or commits) that came right before it, its . Each commit is named by a
hash of its contents, like eb686a5. Following the parents from any commit walks back through the
whole history.
A branch is only a name that points at one commit. main and feature are tiny files inside
.git that each hold a commit hash. Creating a branch copies nothing; it writes one more name.
When you commit on a branch, Git makes the new commit and moves that branch's name to it.
How do HEAD and branches move when you work?
- HEAD is where you are. It usually points at a branch, not at a commit: "you are on main".
git switch featurechanges HEAD to point at feature and updates your files to that branch's commit. - Committing moves only the branch HEAD points at. The new commit's parent is the commit HEAD led to, and that one branch moves forward. Every other branch stays exactly where it was.
- Detached HEAD.
git switch --detach eb686a5(or checking out a commit) points HEAD straight at a commit. New commits made there belong to no branch, so create one withgit switch -c namebefore you leave, or they become hard to find.
What is the difference between a fast-forward and a merge commit?
When you run git merge feature while on main:
- Fast-forward. If main has no commits of its own since feature started, feature's history already contains main's. Git simply moves main forward to feature's commit. No new commit is made, and the history stays a straight line.
- Merge commit. If both branches moved on, as in the lesson, Git combines the two sets of changes since their merge base and records a new commit with two parents: the commit main was on (the first parent) and feature's commit. If both sides changed the same lines, Git stops with a conflict for you to resolve first.
What mistakes do people make with Git branches?
Mini example
Here main has no new commits of its own, so merging fix is a fast-forward: main's pointer just
moves to fix's commit.
git switch -c fix # create fix and point HEAD at itgit commit -am "Fix footer"git switch maingit merge fix# Updating feb24fc..3c6bda4# Fast-forwardQuick check
In the mini example, what does git merge fix do?
Further reading
All steps as text
- Step 1:
git initcreates an empty repository: the hidden .git folder where the history will live. There are no commits yet.Line 1
- Step 2:
git addputs index.html in the staging area, ready for the next commit. Nothing is recorded yet.Line 2
- Step 3:
git commitsaves a snapshot of the staged files: the first commit. Its hash, eb686a5, is its name.Line 3
- Step 4:
The commit also creates main, a pointer to it. HEAD points at main: that is the branch you are on.
Line 3
- Step 5:
git branch featureadds a second pointer to the same commit. Nothing is copied: a branch is just a name for a commit.Line 5
- Step 6:
HEAD still points at main. Creating a branch doesn’t move you onto it.
Line 5
- Step 7:
git switch featuremoves HEAD to feature. Both branches point at the same commit, so your files stay the same.Line 6
- Step 8:
The new commit points back to its parent, the commit HEAD was on. feature, the branch HEAD points at, moves to it; main stays put.
Lines 7–8
- Step 9:
Back on main, HEAD points at main again, and Git puts your files back as they were in eb686a5: nav.html leaves the folder.
Line 10
- Step 10:
This commit’s parent is eb686a5 too. The history has split: main and feature each have a commit the other lacks.
Line 11
- Step 11:
git merge featurefirst finds the last commit both branches share, the merge base: eb686a5.Line 13
- Step 12:
Both sides moved on since then, so Git creates a merge commit with two parents, and moves main, the branch you are on, to it.
Line 13
- Step 13:
feature didn’t move: it still points at 0bc9bff. Its commit is now part of main’s history too.
Line 13
- Step 14:
git log --oneline --graphdraws the same history as text: each commit, its parents, and where HEAD and the branches point.Line 14
- Step 15:
A commit points to its parents, a branch points to a commit, and HEAD points to a branch. Committing moves the branch HEAD is on.