Loading

T7 Git and GitHub · Lesson 3

Branches

A branch is a name for a line of work. Make one for each trial, switch between them in a moment, grow each line on its own, bring a finished line back into main with a fast-forward, and delete the names you no longer need.

  • say what a branch is: a name that points at a commit, kept in a small file
  • make a branch with git branch, and list the branches
  • switch between branches with git switch, and make and switch in one step with git switch -c
  • see every line of work with git log --oneline --all
  • bring a finished branch into main with a fast-forward merge
  • delete a branch with git branch -d, and know when git refuses
About 50 minutesChecked against Git on the laptop (the kit compares every git block with it, in its sandbox) 2.53.0 (2026-11-07); GNU Bash on the laptop (the kit compares every shell block with it, in a sandbox) 5.3.9 (2026-11-07)Play this lesson's games

The back cover

A branch is a name for a line of work. With branches, a trial can grow on its own line while the main work stays safe, and switching between them takes only a moment. Each line keeps its own history.

These pages show what a branch really is, and how to make one and switch to it. They show how to compare two lines of work, and how to bring a finished line back into main with a fast-forward. They also show how to delete and rename branches safely when their work is done.

Before you start

The story: Two plans for one sluice

Two engineers want to change the sluice orders. One wants the gates to open earlier in summer; the other wants a new gate on the east side. Each is sure, and each needs to try the change for a week before anyone agrees.

If both change the one sheet, neither trial will mean anything.

The Keeper takes a fresh ribbon from her desk. "Try each plan on a line of its own," she says. "The main sheet stays as it is until a plan has earned its place."

So far, every commit has gone onto one line: each new version follows the last. Real work is rarely so tidy. Two people try two ideas at once, a fix is needed while a feature is half done, and an experiment must not spoil the version that works. Branches give each of these a line of its own.

It builds on these ideas. Each one has a tiny example, so you can check that you remember it.

What you already knowA tiny exampleWhere it comes back
The three places.desk, tray, cabinetSwitching branches changes the desk and the tray.
Commit names.aba517bA branch points at one commit.
HEAD.cat .git/HEADIt names the branch you are on.
git log --oneline.git log --onelineWith --all, it shows every branch.
Names in brackets.(HEAD -> main)They are branch names.
git status.On branch mainIts first line names the branch.
git commit -am.git commit -am "why"Each line of work grows by commits.
git restore.git restore orders.txtA clean desk makes switching safe.
Small text files.cat fileA branch is one small file in .git.
wc -c.wc -c fileIt measures what a branch costs.

By the end of this lesson you will be able to:

  • say what a branch is: a name that points at a commit, kept in a small file;
  • make a branch with git branch, and list the branches;
  • switch between branches with git switch, and make and switch in one step with git switch -c;
  • see every line of work with git log --oneline --all;
  • bring a finished branch into main with a fast-forward merge;
  • delete a branch with git branch -d, and know when git refuses.

How to follow this lesson

Most blocks start in a repository whose orders.txt holds three lines and one commit, on main. Some start with a branch named summer already made, or with two branches, summer and east. The label above each block says which. Each block starts afresh, so you can run them in any order.

In one line

This lesson is about branches: names for lines of work, cheap to make, quick to switch between, and easy to bring back into main.

What a branch is

At a glance

Think of bookmarks in a long notebook. Each bookmark has a label and marks one page, and moving a bookmark does not change a single page.

A branch is such a bookmark: a name that points at one commit, its tip. Git keeps it in a small file under .git/refs/heads, holding the commit's 40-character name. HEAD names the branch you are on, and each new commit moves that branch forward to itself. Making or moving a branch copies no files at all.

Take a guess: Is every branch really kept as its own small file?

In the Archive, a ribbon marks a place in a long line of sheets. A branch in git is the same idea. It is a name that points at one commit. The commit it points at is called the branch's tip.

Every repository starts with one branch, named main. So far, every commit in these lessons has gone onto it. Here is how git keeps a branch: as one small text file inside .git, named after the branch, which holds the full name of its tip. HEAD, the file you met in lesson 01, says which branch you are on:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main

cat .git/HEAD
cat .git/refs/heads/main
ref: refs/heads/main
aba517bd0ac20cf6722879cf47522f787e978a68

Read it as a chain of two links. HEAD points at the branch main. The branch main points at the commit aba517b. To list the branches, use git branch. It marks the branch you are on with a star:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main

git branch
* main

A commit moves the branch

When you commit, git makes the new commit and then moves the current branch to it. That is all. Watch main's file before and after one commit:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main

cat .git/refs/heads/main
echo "open gate 6" >> orders.txt
git commit -q -am "Open gate 6"
cat .git/refs/heads/main
aba517bd0ac20cf6722879cf47522f787e978a68
8f039b6a2b1135b54b667485a05cfa5afb8d1a59

The new commit, 8f039b6, has aba517b as its parent, so the line of history grew by one. Then git wrote the new commit's name into main's file. The ribbon moved to the newest sheet.

WordMeansIn this repository
brancha name that points at one commitmain
tipthe commit that a branch points ataba517b
HEADwhich branch you are onref: refs/heads/main
current branchthe branch that HEAD names; new commits go onto itmain

What a branch costs

A branch is one small file, so making one costs almost nothing, however large the project is:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main

wc -c .git/refs/heads/main
41 .git/refs/heads/main

That is 41 bytes: the commit's 40-character name and a line ending. A project of ten thousand files has branch files of exactly the same size. This is why developers make a branch for every trial, however small.

Why it matters

A branch is not a copy of your files. The files live in the commits. A branch only says which commit a line of work has reached, so it costs nothing to make, and nothing to throw away.

Worked example 1

Corin is worried that a branch for each engineer's plan will fill the Archive's drawers, as he thinks each branch copies every sheet. Show him what a branch really holds.

What we have: a repository with one commit, on main

What we need: what main's file holds, and how big it is

The plan: read the branch file, measure it, and compare it with git log

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main

cat .git/refs/heads/main
wc -c .git/refs/heads/main
git log --oneline
aba517bd0ac20cf6722879cf47522f787e978a68
41 .git/refs/heads/main
aba517b (HEAD -> main) First orders
  1. The file holds one line: a commit's name. Its first 7 characters, aba517b, are the commit that git log shows. Nothing else is in it.

A branch holds one commit's name, in 41 bytes, whatever the size of the project.

Check: the name in the file starts with aba517b, the commit that git log lists.

A path that fails: looking inside .git/refs for copies of orders.txt. There are none: the files are kept in the commits, and a branch only points at one.

When you are stuck: you are not sure which branch you are on, or what a branch holds

Run git branch: the star marks the current branch. git status also says On branch, then the name.

Run cat .git/HEAD to see the first link of the chain, and cat .git/refs/heads/main for the second.

A branch is never a folder of files. To see what a branch's tip holds, use git show with the branch's name.

Your turn

cat .git/HEAD
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
ref: refs/heads/main
  1. HEAD names the current branch.
  2. In a new repository, that is main.

It prints ref: refs/heads/main.

You make two more commits on main. Where does main point now?Tap to reveal
  1. Each commit moves the current branch to the new commit.

At the newest of the two commits.

A project has 500 files. How many bytes does a new branch file hold?Tap to reveal
  1. A branch file holds a commit's 40-character name and a line ending.

41 bytes, as for any project.

Did you know?

Git's killer feature

Branching is what many people praise git for. The Pro Git book says some call git's branching model its killer feature, the thing that sets it apart from other version control systems. In many older tools a branch meant making a new copy of the source code folder, which could take a long time. In git it is nearly instantaneous, so teams branch and merge often, even several times a day.

Why it fits here: It shows why this topic's branches matter: in git a branch is only a name, so making one is nearly instant.

Source: Pro Git: Branches in a Nutshell

Not always a file

A branch is not always its own file. The git pack-refs manual says branch and tag tips were traditionally stored one file per ref under .git/refs. With hundreds or thousands of tags, that wastes space and slows git down, so pack-refs stores them all in one file, .git/packed-refs. A ref missing from the folders is looked up in that file instead.

Why it fits here: It answers this topic's question: many branches and tags can live together in one file, .git/packed-refs.

Source: git manual: git-pack-refs

In one line

A branch is a name that points at one commit, its tip, kept in a 41-byte file under .git/refs/heads. HEAD names the current branch, and each commit moves the current branch to the new commit.

Making a branch

At a glance

Before starting a risky trial, you mark where you are with a new name. The trial can then grow on its own line.

The command git branch with a name makes a new branch at the current commit, or at a commit you write after the name. It leaves you where you were; making a branch does not switch to it. On its own, git branch lists the branches, with a star beside the current one. Branch names cannot hold spaces, and a slash is allowed.

Take a guess: Why can a branch name not contain ~, ^ or a colon?

The command git branch summer makes a new branch named summer. It points at the commit you are on now. It does not move you: you stay on main.

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main

git branch summer
git branch
* main
  summer

The star is still on main. Both names now point at the same commit, as git log shows in its brackets:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main

git branch summer
git log --oneline
cat .git/refs/heads/summer
aba517b (HEAD -> main, summer) First orders
aba517bd0ac20cf6722879cf47522f787e978a68

HEAD -> main means that HEAD is on main. The name summer, after the comma, is a second name for the same commit. Nothing was copied: git wrote one more 41-byte file, holding the same commit's name.

Good names

A branch's name should say what its line of work is for: summer, east-gate, fix-login. A name may hold slashes, and many teams use them to group names, as in feature/east-gate. Spaces and a few marks are not allowed:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main

git branch 'east gate'
fatal: 'east gate' is not a valid branch name
hint: See 'git help check-ref-format'
hint: Disable this message with "git config set advice.refSyntax false"

The two hint lines are git's advice. The first names the help page with the full rules; the second says how to turn this advice off. Here are the rules that people meet:

Not allowed in a branch nameExample
a spaceeast gate
two dots in a rowa..b
any of ~ ^ : ? * [ and \fix?
a - at the start-x
a / or . at the endeast/

Each name can belong to only one branch:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main

git branch summer
git branch summer
fatal: a branch named 'summer' already exists

Starting from an earlier commit

Write a commit after the name, and the new branch starts there, instead of at the current commit. This block first makes a second commit on main; then the branch before-6 starts at the first commit:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main

echo "open gate 6" >> orders.txt
git commit -q -am "Open gate 6"
git branch before-6 HEAD~1
git log --oneline --all
8f039b6 (HEAD -> main) Open gate 6
aba517b (before-6) First orders

The option --all makes git log show every branch, not only the current one; the section after next explains it. HEAD~1, from lesson 02, names the commit before the latest.

Worked example 2

Keeper Hollis wants one line of work for each engineer's plan: summer and east. Make both branches, and check where they point.

What we have: a repository with one commit, on main

What we need: two new branches, summer and east, and where each points

The plan: git branch twice, then git branch to list them, and git log for the commit they point at

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main

git branch summer
git branch east
git branch
git log --oneline
  east
* main
  summer
aba517b (HEAD -> main, summer, east) First orders
  1. The list is in alphabetical order, with the star on main, where you still are. The brackets name all three branches for aba517b. The order of the names in the brackets carries no meaning.

Both summer and east point at aba517b, the same commit as main.

Check: git log has one commit, and all three names are on it.

A path that fails: expecting to be on east after git branch east. git branch only makes the name; you are still on main, as the star shows.

When you are stuck: git branch refused, or did not do what you expected

is not a valid branch name: take out spaces and the marks in the table above.

already exists: choose another name, or switch to the branch that has it.

not a valid object name: the commit you wrote after the name does not exist; check it with git log --oneline.

You are still on the old branch: that is right. git branch never switches; the next section does.

Your turn

git branch east
git branch
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
  east
* main
  1. git branch east makes the name, and leaves you on main.
  2. The list is in alphabetical order, and the star marks main.

It prints east, then * main.

Right after git branch summer, which branch are you on?Tap to reveal
  1. git branch makes a name, and does not move HEAD.

Still main.

Is fix/east gate a good branch name?Tap to reveal
  1. The slash is allowed.
  2. The space is not.

No: it holds a space. fix/east-gate is fine.

Did you know?

One branch for each change

GitHub's own way of working starts with a branch. Its guide calls GitHub flow a lightweight, branch-based workflow, used at GitHub even for its site policy, documentation and roadmap. The first step is to create a branch with a short, descriptive name, such as increase-test-timeout, so that others can see ongoing work at a glance. The branch is a space to work without affecting the default branch.

Why it fits here: It shows this topic's git branch in daily use: GitHub's own workflow begins every change on a new, well-named branch.

Source: GitHub Docs: GitHub flow

Why ~, ^ and : are banned

The rules for branch names have reasons. The git check-ref-format manual says they make names easy for shell scripts to read, keep the shell from expanding a name typed without quotes, and avoid confusing expressions. A double dot already means a range, as in ref1..ref2. A tilde and a caret already mean parents, and a colon already pairs two refs in fetch and push.

Why it fits here: It answers this topic's question: ~, ^ and : already have meanings in git's names for commits, so a branch cannot use them.

Source: git manual: git-check-ref-format

In one line

The command git branch with a name makes a new branch at the current commit, or at a commit you write after the name, and leaves you where you were. On its own, git branch lists the branches, with a star on the current one.

Switching branches

At a glance

Making a branch only adds a name. To work on that line, you switch to it.

The command git switch with a branch's name moves HEAD to that branch, and makes the desk and the tray match its tip. The option -c makes a branch and switches to it in one step, and git switch - goes back to the branch you were on. If a switch would lose changes on the desk, git refuses. The older git checkout does the same jobs.

Take a guess: Can you jump back two branches, not just one?

To work on a branch, switch to it. The command git switch summer moves HEAD to summer. It also makes the desk and the tray match summer's tip:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main

git branch summer
git switch summer
cat .git/HEAD
git status | head -n 1
Switched to branch 'summer'
ref: refs/heads/summer
On branch summer

HEAD now names summer, so new commits go onto summer. Here the summer plan opens gate 4 at ten, not at noon. The -q, for quiet, leaves out the Switched line:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main

git branch summer
git switch -q summer
sed -i 's/at noon/at ten/' orders.txt
git commit -q -am "Summer: open gate 4 at ten"
git log --oneline
d223728 (HEAD -> summer) Summer: open gate 4 at ten
aba517b (main) First orders

The new commit d223728 is on summer. The name main stayed at aba517b, as it should: main's work did not change.

The desk follows the branch

Switch back to main, and git rewrites orders.txt to match main's tip. Switch to summer again, and the summer version comes back:

In the same repository, with a branch summer whose one commit opens gate 4 at ten; main is checked out

head -n 1 orders.txt
git switch -q summer
head -n 1 orders.txt
git switch -q main
head -n 1 orders.txt
open gate 4 at noon
open gate 4 at ten
open gate 4 at noon

Nothing is lost when you switch. Each version lives in its own commit, and switching only chooses which one is on the desk.

Make and switch in one step

Making a branch and switching to it is so common that git switch does both. git switch -c east makes the branch east at the current commit and switches to it; -c stands for create. Write a commit after the name to start it elsewhere, as with git branch:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main

git switch -c east
git branch
Switched to a new branch 'east'
* east
  main

Back to where you were

git switch -, with a dash, goes back to the branch you were on before. It works like cd - for folders:

In the same repository, with a branch summer whose one commit opens gate 4 at ten; main is checked out

git switch -q summer
git switch -
git switch -
Switched to branch 'main'
Switched to branch 'summer'

Changes on the desk travel with you

You may switch with changes that are not committed yet. If a changed file is the same on both branches, git carries the change across, and lists it first: M for modified, a tab, then the file's name. Here notes.txt is the same on main and summer:

In a repository whose first commit holds orders.txt and notes.txt, with a branch summer that opens gate 4 at ten; main is checked out

echo 'pump 8 low' >> notes.txt
git switch summer
git status -s
M	notes.txt
Switched to branch 'summer'
 M notes.txt

The change is still on the desk, not committed, now on summer. If a change would be lost instead, because the two branches hold different versions of that file, git refuses to switch. The Mistakes section shows that message.

The older command: git checkout

Before git switch existed, git checkout did this job. You will meet it in older guides and scripts, and it still works:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main

git checkout -b east
git checkout main
Switched to a new branch 'east'
Switched to branch 'main'
TodayThe older formWhat it does
git switch summergit checkout summerswitch to a branch
git switch -c eastgit checkout -b eastmake a branch, and switch to it
git switch -git checkout -go back to the last branch
git restore orders.txtgit checkout -- orders.txtthrow away a change on the desk

The command git checkout does two different jobs: it switches branches, and it puts files back. That made mistakes easy, so git switch and git restore were made to split the two jobs. Use the new pair; read the old command when you meet it.

Worked example 3

Try the summer plan on a line of its own: make the branch and switch to it, open gate 4 at ten instead of noon, and commit. Then show that main still has the old orders.

What we have: a repository with one commit, on main

What we need: a commit on a new branch summer, and main's first line unchanged

The plan: git switch -c, then sed and git commit -am; then switch back and read the first line

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main

git switch -c summer
sed -i 's/at noon/at ten/' orders.txt
git commit -q -am "Summer: open gate 4 at ten"
git switch main
head -n 1 orders.txt
Switched to a new branch 'summer'
Switched to branch 'main'
open gate 4 at noon
  1. The commit went onto summer, because HEAD named summer when it was made. Back on main, git put main's version of orders.txt on the desk.

On main, the first line is still open gate 4 at noon; the summer plan waits on its own branch.

Check: git switch -q summer, then head -n 1 orders.txt, would print open gate 4 at ten.

A path that fails: editing orders.txt on main and committing there. The trial would then be on the main line, before anyone agreed to it.

When you are stuck: git switch will not switch, or you are not where you expected

invalid reference: no branch has that name; git branch lists the names.

Your local changes ... would be overwritten: commit the change, or throw it away with git restore, then switch.

a branch is expected, got commit: git switch takes the name of a branch; the Mistakes section shows what a commit's name does.

To see where you are, run git branch --show-current, which prints only the current branch's name.

Your turn

git switch -c east
git branch --show-current
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
Switched to a new branch 'east'
east
  1. git switch -c makes east and switches to it, and says so.
  2. git branch --show-current prints the current branch's name.

It prints Switched to a new branch 'east', then east.

You are on summer. Name two commands that take you back to main.Tap to reveal
  1. One names the branch.
  2. One goes back to the branch you were on before.

git switch main and, if main was the last branch, git switch -.

After git switch summer, whose version of orders.txt is on the desk?Tap to reveal
  1. Switching makes the desk and the tray match the branch's tip.

summer's: the one in summer's tip commit.

Did you know?

@{-2} and the dash

The dash in git switch - is a shortcut. The git switch manual says @{-N} names the Nth last branch you switched to, and that - means the same as @{-1}. So git switch @{-2} goes back two switches. The manual adds that the dash is often used to move quickly between two branches, or to undo a switch made by mistake.

Why it fits here: It answers this topic's question: @{-2} goes back two switches, and the dash is the same as @{-1}.

Source: git manual: git-switch

Carry the changes across

A switch that git refuses can be made to carry the changes along. The git switch manual says that with --merge, changes on the desk that clash with the other branch are stashed before the switch and put back afterwards. If they do not overlap with the differences between the branches, the switch goes ahead with no stash at all. A clash on putting them back leaves them saved in the stash list.

Why it fits here: It adds to this topic's refused switch: git switch --merge stashes the clashing changes and puts them back on the new branch.

Source: git manual: git-switch

In one line

The command git switch with a branch's name moves HEAD to it and makes the desk and the tray match its tip; git switch -c makes a branch and switches to it; git switch - goes back. Changes that would be lost stop a switch; the older git checkout does the same jobs.

Two lines of work

At a glance

Two lines of work that started from the same commit grow apart. Each has commits the other lacks, and a short command shows what one has that the other does not.

Plain git log shows only the current line, git log --all shows every branch, and git log with a branch's name shows that branch's line. The range A..B lists the commits on B that A does not have. The command git branch -v shows each branch with its tip commit and subject.

Take a guess: What do three dots between two branch names mean, instead of two?

Now both plans have a commit of their own. Each grew from the first orders, aba517b. The summer plan opens gate 4 at ten; the east plan adds gate 9. Drawn as a picture, with the newest commits on the right, the history looks like this:

The history, drawn

         o d223728  summer: Summer: open gate 4 at ten
        /
o aba517b  main: First orders
        \
         o 4ea879c  east: East: open gate 9

There are two lines of work, and main is where both began. Plain git log shows only the line you are on, back from HEAD. On main, that is one commit:

In the same repository, with the branch summer, and a branch east made from main whose one commit adds open gate 9; main is checked out

git log --oneline
aba517b (HEAD -> main) First orders

The option --all shows the commits of every branch, newest first:

In the same repository, with the branch summer, and a branch east made from main whose one commit adds open gate 9; main is checked out

git log --oneline --all
4ea879c (east) East: open gate 9
d223728 (summer) Summer: open gate 4 at ten
aba517b (HEAD -> main) First orders

Give a branch's name instead, and git log shows that branch's line, back to the start:

In the same repository, with the branch summer, and a branch east made from main whose one commit adds open gate 9; main is checked out

git log --oneline east
4ea879c (east) East: open gate 9
aba517b (HEAD -> main) First orders

What one line has that another lacks

Two dots between two names, as in main..east, mean the commits on east that main does not have. That is exactly what joining east into main would bring:

In the same repository, with the branch summer, and a branch east made from main whose one commit adds open gate 9; main is checked out

git log --oneline main..east
echo ---
git log --oneline east..main
4ea879c (east) East: open gate 9
---

The second git log printed nothing at all: main has no commit that east lacks. Read A..B as "what B has that A does not".

Every branch, with its tip

The option -v, for verbose, makes git branch show each branch's tip: its short name and its message:

In the same repository, with the branch summer, and a branch east made from main whose one commit adds open gate 9; main is checked out

git branch -v
  east   4ea879c East: open gate 9
* main   aba517b First orders
  summer d223728 Summer: open gate 4 at ten
CommandShows
git log --onelinethe current line, back from HEAD
git log --oneline --allthe commits of every branch
git log --oneline eastthe line of the branch east
git log --oneline main..eastthe commits on east that main lacks
git branch -veach branch with its tip commit

Worked example 4

Keeper Hollis asks what the east plan would bring into the main orders: which commits, by whom, when, and what each one says.

What we have: the branches main and east, which both grew from aba517b

What we need: the full record of each commit on east that main lacks

The plan: the range main..east, with git log in its full form, which shows the author, the date and the message

In the same repository, with the branch summer, and a branch east made from main whose one commit adds open gate 9; main is checked out

git log main..east
commit 4ea879c5b125a517de51cb4fc6cba5b70e0a04eb (east)
Author: Asha <asha@tower.example>
Date:   Thu Jan 1 10:00:09 2026 +0000

    East: open gate 9
  1. One commit is on east and not on main: 4ea879c, by Asha, with the message East: open gate 9.

The east plan would bring one commit, 4ea879c, East: open gate 9.

Check: git log --oneline main..east lists the same single commit.

A path that fails: plain git log while on main. It shows only main's line, and the east commit is not on it.

When you are stuck: a branch's commits seem to be missing

Plain git log shows only the current line. Add --all, or write the branch's name after git log.

main..east lists what east has and main lacks. Swap the two names for the other way round.

git branch -v shows where each branch's tip is, with its message.

Your turn

git log --oneline --all | wc -l
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
3
  1. --all lists the commits of every branch, each one once.
  2. Here there are the first orders, the summer commit and the east commit.

It prints 3.

In this repository, what does git log --oneline summer..main print?Tap to reveal
  1. It lists the commits on main that summer lacks.
  2. main's only commit, aba517b, is on summer's line too.

Nothing: main has no commit that summer lacks.

Which command lists every branch with its tip commit?Tap to reveal
  1. git branch can show more with an option.

git branch -v.

Did you know?

Two dots and three dots

A range can have three dots as well as two. Git's manual on naming revisions says A...B, the symmetric difference, is the set of commits reachable from either A or B but not from both: what each line has that the other lacks. With two dots, A..B lists only B's side. An end left out means HEAD, so origin.. asks what you have done since you forked from origin.

Why it fits here: It answers this topic's question: A...B lists the commits on either line but not on both, where A..B lists only B's.

Source: git manual: gitrevisions

Drawing the lines

git log can draw the lines it lists. The git log manual says --graph draws a text-based picture of the history on the left of the output, and may print extra lines between commits so that the graph is drawn properly. With --oneline and --all as well, every branch appears as a line of its own, and you can see where lines split and join.

Why it fits here: It adds a picture to this topic's git log --all: --graph draws how the lines of work split and join.

Source: git manual: git-log

In one line

Plain git log shows the current line; --all shows every branch; a branch's name shows that branch's line; A..B lists the commits on B that A lacks. The command git branch -v shows each branch with its tip.

Bringing a line back: fast-forward

At a glance

When a trial on a branch is finished, its work should join main. If main has not moved since the branch began, joining is only a matter of moving a name.

Switch to main and run git merge with the branch's name. Because main has no commits of its own since the branch began, git fast-forwards: it moves main's name forward to the branch's tip and makes no new commit. It then prints a diffstat, a small chart of what changed in each file.

Take a guess: Does GitHub fast-forward when you press its Merge pull request button?

The summer plan has proved itself, and it should join the main orders. To bring a branch's work into another branch, switch to the branch that receives it, and merge the other. Here you are on main, and you merge summer:

In the same repository, with a branch summer whose one commit opens gate 4 at ten; main is checked out

git merge summer
git log --oneline
Updating aba517b..d223728
Fast-forward
 orders.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
d223728 (HEAD -> main, summer) Summer: open gate 4 at ten
aba517b First orders

Read the output line by line:

LineMeans
Updating aba517b..d223728main moves from aba517b to d223728
Fast-forwardthe kind of merge: no new commit was needed
orders.txt | 2 +-orders.txt had 2 lines changed: one added (+), one removed (-)
1 file changed, ...the totals

The third line is a small chart called a diffstat. A rewritten line counts as one line removed and one added, as in every diff. With more changes, the row of + and - marks grows, so you can see at a glance which files changed most.

Why no new commit

A fast-forward happens when the receiving branch has not moved since the other branch began. Here, summer's line is main's line plus one more commit. Git needs no new commit to join them. It moves main's name forward to summer's tip, and updates the desk and the tray to match. That is all a fast-forward is:

A fast-forward, drawn

before:  o aba517b (main) --- o d223728 (summer)
after:   o aba517b --- o d223728 (main, summer)

Both names now point at the same commit. Merging again changes nothing, and git says so:

In the same repository, with a branch summer whose one commit opens gate 4 at ten; main is checked out

git merge -q summer
git merge summer
Already up to date.

Fast-forward only

The option --ff-only tells git to merge only if a fast-forward is possible, and to refuse otherwise. Many teams use it to keep the main line straight. Where a fast-forward is possible, it works as before:

In the same repository, with a branch summer whose one commit opens gate 4 at ten; main is checked out

git merge --ff-only summer | head -n 2
Updating aba517b..d223728
Fast-forward

When both lines have moved on, a fast-forward is not possible, and git must join them with a new commit. That is the next lesson.

Worked example 5

Both plans are on their branches, and the summer plan is approved first. Bring it into main, and show that the east plan is left untouched.

What we have: main at aba517b; summer and east, one commit each, both grown from aba517b

What we need: main moved to summer's tip, with east where it was

The plan: on main, merge summer (a fast-forward, as main has not moved); then look at every branch with --all

In the same repository, with the branch summer, and a branch east made from main whose one commit adds open gate 9; main is checked out

git merge -q summer
git log --oneline --all
4ea879c (east) East: open gate 9
d223728 (HEAD -> main, summer) Summer: open gate 4 at ten
aba517b First orders
  1. main now points at d223728, with summer. east still points at its own commit, 4ea879c, and waits for its own week.

main holds the summer plan, at d223728; the east plan is still only on east.

Check: git log --oneline main..east still lists 4ea879c, the east commit.

A path that fails: merging while on summer, with git merge main. That asks for main's work to come into summer; here git only says Already up to date, and main does not move at all.

When you are stuck: git merge did something you did not expect

Already up to date: the branch you are on already has every commit of the other.

not something we can merge: no branch or commit has that name; check it with git branch.

Your local changes ... would be overwritten by merge: commit the change, or throw it away with git restore, then merge.

Check where you are first: the work comes into the branch with the star in git branch.

Your turn

git merge summer | head -n 1
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
Updating aba517b..d223728
  1. You are on main, at aba517b; summer's tip is d223728.
  2. A fast-forward starts with the Updating line.

It prints Updating aba517b..d223728.

After the fast-forward, how many commits does main's line hold?Tap to reveal
  1. main moved to summer's tip, d223728.
  2. d223728's parent is aba517b.

Two: aba517b and d223728.

When is a fast-forward possible?Tap to reveal
  1. Think of what the receiving branch did while the other branch grew.

When the receiving branch has not moved since the other branch began from it.

Did you know?

GitHub's button does not fast-forward

Many teams keep a record of every merge, even when a fast-forward would do. The git merge manual says --no-ff creates a merge commit in all cases, even when the merge could be a fast-forward. GitHub's default Merge pull request button works that way: its documentation says the pull request is merged using the --no-ff option, so the branch's commits arrive in a merge commit.

Why it fits here: It answers this topic's question: GitHub's default merge button uses --no-ff, so it makes a merge commit instead of fast-forwarding.

Sources: git manual: git-merge; GitHub Docs: About merge methods on GitHub

git-flow, and second thoughts

On 5 January 2010 Vincent Driessen published A successful Git branching model, the scheme that became known as git-flow and grew hugely popular. On 5 March 2020 Driessen added a note of reflection to it. The note says that a team doing continuous delivery of software should adopt a much simpler workflow, like GitHub flow, instead of trying to shoehorn git-flow into the team.

Why it fits here: It puts this topic's branches in a team setting: the author of git-flow now advises simpler branching for teams that release continuously.

Source: Vincent Driessen: A successful Git branching model

In one line

To bring a branch's work into the current branch, run git merge with its name. If the current branch has not moved since the other began, git fast-forwards: it moves the name forward, makes no new commit, and prints a diffstat of what changed.

Deleting and renaming branches

At a glance

A branch whose work has joined main has done its job. Its name can go, and its commits stay in main's history.

The command git branch -d deletes a branch's name and keeps its commits, but refuses when no other branch holds its work. The option -D deletes it anyway, and git branch -m renames a branch. You cannot delete the branch you are on: switch away from it first. Deleting finished branches keeps the list of branches short and easy to read.

Take a guess: Can a branch deleted with -D ever be brought back?

Once summer is merged, its name is no longer needed: main points at the same commit. The command git branch -d summer deletes the name, and keeps every commit:

In the same repository, with a branch summer whose one commit opens gate 4 at ten; main is checked out

git merge -q summer
git branch -d summer
git log --oneline
Deleted branch summer (was d223728).
d223728 (HEAD -> main) Summer: open gate 4 at ten
aba517b First orders

The commit d223728 is still there, on main. Only the 41-byte file went. Git names the commit the branch pointed at, so that you could make the branch again if you needed it.

Git protects unmerged work

The east plan has not been merged. Try to delete it:

In the same repository, with the branch summer, and a branch east made from main whose one commit adds open gate 9; main is checked out

git branch -d east
error: the branch 'east' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D east'
hint: Disable this message with "git config set advice.forceDeleteBranch false"

The east commit is on no other branch. If its name went, no branch would lead to that commit any more, and the work would be as good as lost. So -d refuses. A capital -D deletes the name anyway. Use it only when you mean to throw that line's work away:

In the same repository, with the branch summer, and a branch east made from main whose one commit adds open gate 9; main is checked out

git branch -D east
git branch
Deleted branch east (was 4ea879c).
* main
  summer

You cannot delete the branch you are on. Switch to another branch first:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main

git branch -d main
error: cannot delete branch 'main' used by worktree at '/home/asha/shop'

The message names the repository's folder, /home/asha/shop, which git calls a worktree: the folder whose desk shows the branch.

Renaming a branch

git branch -m old new gives a branch a new name; -m stands for move. With one name only, it renames the current branch:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main

git branch summer
git branch -m summer early-summer
git branch
  early-summer
* main
CommandDoesWhen
git branch -d namedeletes the name; refuses if its work is on no other branchafter a merge
git branch -D namedeletes the name anywayto throw away a failed trial
git branch -m old newrenames a branchwhen the name no longer fits

Worked example 6

The summer plan is merged into main. Tidy up: delete every branch whose work main already holds, and keep the others.

What we have: main, summer and east; summer just merged into main; east not merged

What we need: summer deleted, east kept

The plan: git branch -d with both names: it refuses an unmerged branch and deletes a merged one, which is exactly the rule wanted

In the same repository, with the branch summer, and a branch east made from main whose one commit adds open gate 9; main is checked out

git merge -q summer
git branch -d summer east
git branch
error: the branch 'east' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D east'
hint: Disable this message with "git config set advice.forceDeleteBranch false"
Deleted branch summer (was d223728).
  east
* main
  1. Git checks every name first, and then deletes the ones that passed, so the refusal is printed before the deletion. The branch east holds work that is on no other branch, so git refused, and said why. The branch summer holds nothing that main lacks, so its name went.

summer is deleted, and east is kept, with its work.

Check: git log --oneline --all still shows the east commit and the summer commit.

A path that fails: git branch -D summer east. The capital D would delete east too, and the east plan's work would no longer be on any branch.

When you are stuck: git branch -d refuses

not fully merged: the branch's work is on no other branch. Merge it first, or use -D only if you are sure.

cannot delete branch ... used by worktree: you are on it. Switch to another branch, then delete it.

branch ... not found: no branch has that name; git branch lists them.

Your turn

git branch -d east
echo "[$?]"
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
error: the branch 'east' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D east'
hint: Disable this message with "git config set advice.forceDeleteBranch false"
[1]
  1. The east commit is on no other branch.
  2. So -d refuses, and git ends with the status 1, for a failure.

It prints the not fully merged error with its two hints, then [1].

After git branch -d summer, what has happened to summer's commits?Tap to reveal
  1. -d only deletes a branch whose work another branch holds.

Nothing: they are still on main. Only the name went.

Which command renames the current branch to trunk?Tap to reveal
  1. -m with one name renames the current branch.

git branch -m trunk.

Did you know?

Gone, but remembered for a while

A deleted branch's commits do not vanish at once. Git keeps reflogs, records of when the tips of branches and other refs changed, and the git config manual says reflog entries expire after 90 days by default. Entries for commits that are no longer reachable from the current tip expire sooner, after 30 days. Until then, git reflog can usually find a commit lost with -D.

Why it fits here: It answers this topic's question: for at least 30 days by default, the reflog can usually still find a deleted branch's commits.

Sources: git manual: git-reflog; git manual: git-config (gc.reflogExpire)

Which branches are safe to delete

git branch can list the branches whose work is already in. The git branch manual says --merged lists only branches whose tips are reachable from HEAD, or from a commit you name, and --no-merged lists the others. Every branch that git branch --merged prints, apart from the one you are on, can be deleted with -d without losing work.

Why it fits here: It adds a check to this topic's -d: git branch --merged lists the branches whose work is already safe.

Source: git manual: git-branch

In one line

The command git branch -d deletes a branch's name and keeps its commits, but refuses when no other branch holds its work; -D deletes it anyway, and -m renames a branch. You cannot delete the branch you are on.

Where it is used

Branches are how almost every team works with git. Here is where each idea of this lesson does its work.

What you learnedWhere it is usedA real example
a branch for each piece of workevery team's daily worka branch fix-login for one bug, while main stays releasable
git switchmoving between pieces of workleaving a half-done feature to fix an urgent bug
git log --oneline --allseeing where every line has got tobefore deciding what to merge
main..featurechecking what a merge would bringa review of a branch before it joins main
fast-forward mergeskeeping the main line straightteams that merge only with --ff-only
git branch -dtidying up after a mergedeleting each feature branch once it is in main

Hosting services such as GitHub are built around branches too: a pull request, which lesson 06 explains, is a request to merge one branch into another.

Mistakes and debugging

These are the slips that people meet most often with branches. Every output below is the real one, from the code shown.

1. Switching with a change in the way

In the same repository, with a branch summer whose one commit opens gate 4 at ten; main is checked out

echo 'oops' >> orders.txt
git switch summer
error: Your local changes to the following files would be overwritten by checkout:
	orders.txt
Please commit your changes or stash them before you switch branches.
Aborting

The file orders.txt differs between main and summer, so switching would overwrite the change on the desk. Git refuses, and nothing moves. The fix: commit the change, or throw it away with git restore orders.txt, then switch. Stashing, the third way, comes in lesson 07.

2. Making a branch, then committing without switching

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main

git branch summer
sed -i 's/at noon/at ten/' orders.txt
git commit -q -am "Summer: open gate 4 at ten"
git log --oneline
36f6033 (HEAD -> main) Summer: open gate 4 at ten
aba517b (summer) First orders

The command git branch made the name but did not switch, so the commit went onto main, and summer stayed behind. The fix: use git switch -c summer, which makes the branch and switches in one step. Moving a commit that landed on the wrong branch needs git reset, which lesson 07 teaches.

3. A misspelt branch name

In the same repository, with a branch summer whose one commit opens gate 4 at ten; main is checked out

git switch sumer
fatal: invalid reference: sumer

No branch is named sumer. The fix: list the names with git branch, and copy the name exactly.

4. Merging the wrong way round

In the same repository, with a branch summer whose one commit opens gate 4 at ten; main is checked out

git switch -q summer
git merge main
Already up to date.

The aim was to bring summer into main, but this asked for main to come into summer, and summer already has all of main. Nothing happened. The fix: switch to the branch that should receive the work, git switch main, then git merge summer.

5. Checking out a commit instead of a branch

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main

git checkout -q aba517b
git status | head -n 1
git branch
HEAD detached at aba517b
* (HEAD detached at aba517b)
  main

Given a commit's name, git checkout puts HEAD on the commit itself, not on a branch. This is called a detached HEAD. It is useful for looking at an old version, but new commits made there belong to no branch. Without -q, git prints a long note that explains this. The fix: go back with git switch main. If you made commits there that you want to keep, first run git switch -c name, which puts them on a new branch.

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main

git checkout -q aba517b
git switch main
Switched to branch 'main'

The debugging corner: where am I?

When a branch command surprises you, ask git where you are before anything else. git branch gives the star; git status gives On branch, or HEAD detached; git log --oneline --all shows every line, with the names in brackets.

Quick card

The whole lesson on one card. Come back to it whenever you need a reminder.

The lesson in code

git branch summer                 # a new name at this commit
git switch -q summer              # go onto it
sed -i 's/at noon/at ten/' orders.txt
git commit -q -am "Summer: open gate 4 at ten"
git switch -q -c east main        # another line, from main
echo "open gate 9" >> orders.txt
git commit -q -am "East: open gate 9"
git log --oneline --all           # every line
git switch -q main
git merge summer | head -n 2      # a fast-forward
git branch -d summer              # the name goes; the commit stays
git branch -v
7411612 (HEAD -> east) East: open gate 9
d223728 (summer) Summer: open gate 4 at ten
aba517b (main) First orders
Updating aba517b..d223728
Fast-forward
Deleted branch summer (was d223728).
  east 7411612 East: open gate 9
* main d223728 Summer: open gate 4 at ten
When you seeIt meansDo
Switched to branch 'x'you are on x nowcarry on
Switched to a new branch 'x'x was made, and you are on itcarry on
fatal: invalid reference: xno branch is named xgit branch for the names
would be overwritten by checkouta change on the desk is in the waycommit it, or git restore
Fast-forwardthe name moved forward; no new commitnothing to fix
Already up to date.nothing new to bring inare you on the right branch?
not fully mergedits work is on no other branchmerge it, or -D if you are sure
HEAD detached atHEAD is on a commit, not a branchgit switch main

The story: A line of its own

You start a new line of pictures from the current orders for each plan, and switch between them as you would turn between two copies, except that nothing is copied. Each plan grows on its own line. When the summer plan proves itself, you bring its line back into the main one.

The east gate's line waits, untouched, for its own week.

"A line costs nothing," says the Keeper. "An untried change on the main sheet can cost a tunnel."

Reagents gathered: branch, HEAD, git switch, main, fast-forward, git branch.

Sources