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
| Word | What it means |
|---|---|
| Branch | a name that points at one commit |
| Tip | the newest commit on a branch |
| Switch | to move your files to another branch |
| HEAD | the branch you are working on now |
| Commit graph | a 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
git init -b main trailShown 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
printf 'Latte 3.20\n' > trail/prices.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Stage the file
git -C trail add prices.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Save the first commit
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
git -C trail branchShown 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
git -C trail switch -c late-hoursShown 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
printf 'Cake 3.50\n' >> trail/prices.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Commit it on the branch
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
git -C trail branchShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Switch back to main
git -C trail switch mainShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Read the file on main
cat trail/prices.txtShown 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
| Branch | Commits it has | In main..late-hours? |
|---|---|---|
| main | Add prices | No |
| late-hours | Add prices, Add cake | Only Add cake |
The cells students fill in are left blank in this preview.
List what late-hours has and main lacks
git -C trail --no-pager log --format=%s main..late-hoursShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.