Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Git branches: create and switch

After this lesson: I can work on a branch and switch between branches.

55 min lesson12 commandsGit and Gitea lab: learn clone, commit and push on a Git server of your own

Why Git has branches

A branch in Git is a separate line of work with commits of its own. It lets you try a change without touching the main line, so unfinished work never breaks the version people rely on.

A cafe wants to test new opening hours. The owner does not want the printed menu to change until the test works. A branch gives the test its own space.

By the end of this lesson

Note

I can work on a branch and switch between branches.

Key words

WordWhat it means
Brancha name that points at one commit
Tipthe newest commit on a branch
Switchto move your files to another branch
HEADthe branch you are working on now
Commit grapha drawing of the commits and how they connect

The cells students fill in are left blank in this preview.

A branch is a name on a commit

A branch is only a name that points at one commit. That commit is its tip. When you commit on a branch, its name moves to the new commit. Every other branch stays where it was.

The diagram below shows two branches. main points at the commit Add prices. late-hours points at a newer commit that follows it.

Make a repository called trail

Command
git init -b main trail

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Make a file with one price

Command
printf 'Latte 3.20\n' > trail/prices.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Stage the file

Command
git -C trail add prices.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Save the first commit

Command
git -C trail commit -m "Add prices"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

List the branches

Command
git -C trail branch

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Switching branches changes your files

The command git switch -c late-hours makes a new branch and moves onto it. The -c stands for create.

Commits you make now go on late-hours. When you switch back to main, Git puts the files back as main had them. Your files change because the branch changed.

Make a branch and move onto it

Command
git -C trail switch -c late-hours

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Add a second price on the branch

Command
printf 'Cake 3.50\n' >> trail/prices.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Commit it on the branch

Command
git -C trail commit -am "Add cake"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

List the branches again

Command
git -C trail branch

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Switch back to main

Command
git -C trail switch main

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Read the file on main

Command
cat trail/prices.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think a branch is a full copy of the project folder. It is not. A branch is only a name that points at a commit. That is why making one is instant, and why the files you see change when you switch.

Two dots ask which commits a branch has

Each commit knows its parent, the commit before it. A branch has a commit if you can reach it from the tip by following parents.

The form main..late-hours means: the commits that late-hours has and main lacks. Here that is the one commit, Add cake.

Worked example: who has which commit

BranchCommits it hasIn main..late-hours?
mainAdd pricesNo
late-hoursAdd prices, Add cakeOnly Add cake

The cells students fill in are left blank in this preview.

List what late-hours has and main lacks

Command
git -C trail --no-pager log --format=%s main..late-hours

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

About this lab and its source: Git and Gitea lab: learn clone, commit and push on a Git server of your own

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.