What a branch is
Last updated
A branch sounds big, like a copy of the whole project. It's the opposite: a branch is just a name that points at one commit, and moves along when you commit. In this lesson you'll see that, make a branch, and see why making one costs almost nothing.
By the end of this lesson you can
- Explain a branch as a movable name pointing at a commit
- Explain HEAD as a pointer to the branch you're on
- List branches with git branch and git branch -v, and make one
A branch is a label
Look at the graph beside any sandbox so far: a line of commits, and a label, main, on the newest. That label is a branch. A branch is a name that points at one commit. That's all it is.
When you commit, Git makes the new commit and moves the branch label onto it. That's how main always ends up on the newest commit.
$ git branch -v * main 67e435d Cover the leche flan with foil
67e435dHEAD →main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
Because a branch is just a name for a commit, making one is instant and takes no space. Git keeps it as a tiny file holding a 40-character hash (Stage 8 shows it).
HEAD: where you are
HEAD is Git's name for "where you are now". Usually HEAD points at a branch, and the branch points at a commit. When you commit, the branch HEAD points at is the one that moves.
git branch lists branches, with * beside the one HEAD points at. git branch -v adds each one's commit. In this course's graph, the branch HEAD is on is filled Pink and labelled HEAD →, and the commit it's on is a Pink circle.
Make a branch
git branch <name> makes a new branch pointing at the commit you're on. It doesn't switch to it: HEAD stays where it was. (Lesson 3.2 switches.)
- List the branches with
git branch - Make a branch called
desserts - List them again with
git branch -v: both point at the same commit
Working folder
no changes
Staging area
nothing staged
Repository
10 commits on main
You are in ~/kusina, which has ten commits from two weeks of work.
Commit graph
67e435dHEAD →main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
Branch names can't have spaces. Teams usually use short lowercase names with dashes, like desserts or fix-flan-eggs.
Spot the trap: "a branch is a copy of the folder"
Joaquin says: “Don’t make too many branches, Ana. Each one copies all the recipes, so the repository will get huge.”
What happens if…
Check what you learned
Sources
Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.
- Pro Git: Branches in a Nutshell: a branch is a lightweight movable pointer to a commit
- Git documentation: git-branch: listing, creating and -v
- Git documentation: gitglossary, branch and HEAD: the official meanings
- Learn Git Branching (Peter Cottle): an interactive way to see branches move, credited as inspiration