BeginnerLesson 7 of 305 min read

Branches: Working on Ideas Without Risk

Create a branch, watch your files change as you switch, and see that a branch is only a tiny label pointing at a commit.

In this lesson you will

  • Explain what a branch really is
  • Create and switch branches
  • Read the commit graph
  • Name branches sensibly

About 28 minutes

Trying an idea without risk

Nia wants to add herb pots to the plan, but the other volunteers are using the plan right now, and she is not sure the idea will work. She does not want half finished herbs in the version everybody trusts. In Git, the answer is a branch: a separate line of work that starts from where you are, and that can be kept, merged back, or thrown away without touching the main line.

A branch is only a label

Here is the surprise. A branch is not a copy of your files. It is a small movable label that points at one commit. When you make a new commit, the label under your feet moves forward to it. That is why branches are cheap, and why Git users make them all the time. The special name HEAD, which you met in lesson 5, says which branch you are standing on.

Three commands cover most of the work. git branch lists the branches, with a star beside the current one. git switch -c name creates a branch and moves onto it. And git switch name moves to an existing one. Older guides use git checkout for these jobs. Git introduced switch in version 2.23 to separate the tasks that checkout mixed together, and both still work.

Read the next session carefully, because it shows something that feels like magic. Nia creates the branch, adds herbs on it, and prints the plan. Then she switches back to main and prints the plan again.

$ mkdir garden
$ cd garden
$ git init
Initialized empty Git repository in /home/nia/garden/.git/
$ echo "Beans: row 1" > plan.txt
$ git add plan.txt
$ git commit -m "Add planting plan"
[main (root-commit) cd4328a] Add planting plan
 1 file changed, 1 insertion(+)
 create mode 100644 plan.txt
$ echo "Carrots: row 2" >> plan.txt
$ git commit -am "Add carrots"
[main d9d4004] Add carrots
 1 file changed, 1 insertion(+)
$ git branch
* main
$ git switch -c add-herbs
Switched to a new branch 'add-herbs'
$ echo "Herbs: pots by the door" >> plan.txt
$ git commit -am "Add herb pots"
[add-herbs 14eb940] Add herb pots
 1 file changed, 1 insertion(+)
$ cat plan.txt
Beans: row 1
Carrots: row 2
Herbs: pots by the door
$ git switch main
Switched to branch 'main'
$ cat plan.txt
Beans: row 1
Carrots: row 2
$ echo "Mon: Nia" > rota.txt
$ git add rota.txt
$ git commit -m "Add the rota"
[main 637db8c] Add the rota
 1 file changed, 1 insertion(+)
 create mode 100644 rota.txt
$ git branch
  add-herbs
* main
$ git log --oneline --graph --all --decorate
* 637db8c (HEAD -> main) Add the rota
| * 14eb940 (add-herbs) Add herb pots
|/  
* d9d4004 Add carrots
* cd4328a Add planting plan
$ wc -c .git/refs/heads/main .git/refs/heads/add-herbs
41 .git/refs/heads/main
41 .git/refs/heads/add-herbs
82 total

The same file shows different contents depending on the branch. On the herb branch the plan ends with the herb pots. On main the herbs have vanished, and they come back when she switches again. Git swapped the working folder to match each branch. The same happens to whole files: the rota.txt file that Nia created on main does not exist on the herb branch.

Seeing the shape of your history

The graph command near the end of the session draws the branches. After the shared commit, the history splits in two: one commit on main adds the rota, and one commit on add-herbs adds the pots. A word in brackets such as (HEAD -> main) marks where each label points. Both branches share the earlier commits, and nothing was copied.

Proof that a branch is tiny

Git stores each branch as a file inside .git. The last command in the session measured two of them, and each is 41 bytes: forty characters of commit ID plus a line break. The short program below reads the same files without asking Git, and prints the first seven characters of each.

import pathlib
heads = pathlib.Path("/home/nia/garden/.git/refs/heads")
for f in sorted(heads.iterdir()):
    print(f.name + ":", f.read_text().strip()[:7])
add-herbs: 14eb940
main: 637db8c

Compare those seven characters with the codes at the front of the branch lines in the graph. They match, because the branch is the commit ID. A repository can hold hundreds of branches at almost no cost.

Naming and habits

  • Use short names in lower case, with hyphens between words, such as add-herbs.
  • Name a branch after its purpose, not after yourself or the date.
  • Keep main in a working state, and do experiments on branches.
  • Check git status before you switch, so you know your files are saved.
Common mistake: committing on the wrong branch

Forgetting which branch you are on, and committing to main by accident. Run git branch or read the first line of git status before you start work.

Switch with a tidy folder

The plan and rota are made up, and the commit codes will differ on your computer. If you switch branches with unsaved changes, Git may carry them across or refuse, depending on the files, so commit or set aside your work first.

Make a branch called practice, change a file, switch back, and watch the file change. Then run the graph command from this lesson and find both branch labels.

Practice for this lesson

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

What is a branch in Git?

  1. A full copy of the project
  2. A backup on GitHub
  3. A movable label that points at a commit
  4. A kind of file

Each branch file was 41 bytes, just a commit ID.

Which command creates a branch and moves onto it?

  1. git switch -c add-herbs
  2. git branch -d add-herbs
  3. git merge add-herbs
  4. git commit add-herbs

switch with -c creates the new branch and switches to it.

Why did plan.txt change when Nia switched back to main?

  1. The file was deleted
  2. The branch was merged
  3. Git set the working folder to match that branch
  4. GitHub rewrote it

The herbs showed on one branch and not on the other.

What does HEAD mean?

  1. Where you are now, the branch and commit you are on
  2. The first commit
  3. The remote
  4. A file in .git

The graph showed HEAD pointing at main.

Why is it safe to create many branches?

  1. Git deletes old ones
  2. They are tiny labels, not copies of the files
  3. They are stored online only
  4. They cannot be changed

Each branch was just 41 bytes.

Summary

  • A branch is a movable label pointing at a commit.
  • git switch -c creates and enters a branch; git switch moves between them.
  • Your working folder changes to match the branch you switch to.
  • Branches are tiny, so use them freely and keep main working.

Next lesson

Lesson 8 brings branches back together with merging, and shows how to fix conflicts.

Questions

Should I use checkout or switch?

Either works. switch and restore were introduced in Git 2.23 to split up what checkout did, and they are easier to read. Older guides still use checkout.

What if I switch branches with unsaved changes?

Git carries them over if it can do so without a clash, and refuses if it cannot. The safest habit is to commit, or set the work aside, before switching.

How many branches are too many?

There is no hard limit. Delete branches once they are merged, so your list stays short and meaningful.

Progress is saved in this browser.