Branches · lesson 1 of 7
Commands and ideas in this lesson
ABrancha name pointing at a commit
BHEADwhich branch you're on
Cgit branchlist or make branches

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
  1. 67e435d HEAD →main Cover the leche flan with foil
  2. ba04ca2 Add lumpiang shanghai recipe
  3. 98a5edc Add a script that builds the index page
  4. d078487 Make the sinigang steps clearer
  5. 145dc1e Add pancit bihon recipe
  6. 52d1f4d Say how much salt goes in the adobo
  7. 1c17fad Add a README
  8. 9c53888 Add leche flan recipe
  9. 1f359a2 Add sinigang recipe
  10. 601c654 Add adobo recipe
1 of 3 main points at “Cover the leche flan with foil”. HEAD points at main: you’re on main.

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

Git sandbox
  1. List the branches with git branch
  2. Make a branch called desserts
  3. 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

  1. 67e435d HEAD →main Cover the leche flan with foil
  2. ba04ca2 Add lumpiang shanghai recipe
  3. 98a5edc Add a script that builds the index page
  4. d078487 Make the sinigang steps clearer
  5. 145dc1e Add pancit bihon recipe
  6. 52d1f4d Say how much salt goes in the adobo
  7. 1c17fad Add a README
  8. 9c53888 Add leche flan recipe
  9. 1f359a2 Add sinigang recipe
  10. 601c654 Add 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"

Spot the trapJoaquin

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

What is a branch in Git?
You run git branch desserts. Which branch are you on now?
You’re on main and make a commit. What moves?

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.

All sources for Simple Git