Why Branches Exist
So far every commit has gone onto one line of history, main. Real work is
rarely that tidy: you start a new feature, a bug report arrives, a teammate
wants to try an idea. Branches let each piece of work grow separately and be
combined later, which the next lesson covers.
A Branch Is a Name for a Commit
A branch is not a copy of your files. It is a name that points at one commit, the newest commit on that line of work, called the branch's tip. When you commit on a branch, Git makes the new commit and moves that name forward to it. Nothing else moves.
HEAD is how Git records where you are. Normally HEAD points at a branch, meaning "I am on this branch", and that branch's tip is the commit your files come from.

Walk through the diagram:
mainandphotosstart on the same commit, B. HEAD points atphotos.- A commit on
photoscreates C. Onlyphotosmoves to C, and HEAD comes along because it points atphotos.mainstays on B. - In the last panel HEAD points straight at a commit instead of a branch. This is a detached HEAD. A new commit D moves only HEAD; no branch name records it.
Working With Branches
git branch # list branches; * marks the one you are on
git branch <name> # create a branch at the current commit
git switch <name> # move to that branch
git switch -c <name> # create a branch and move to it
Switching changes the files in your folder to match the tip of the branch you switch to. A file committed only on one branch disappears when you switch away and comes back when you return. It is not lost; it lives in that branch's commits.
If you have uncommitted edits, Git carries them along when the switch does not
touch those files. If it would have to overwrite them, Git refuses with
Your local changes to the following files would be overwritten by checkout.
Commit or discard the edits first; a later lesson shows another way.
To see every branch at once, ask git log for all of them, drawn as a graph:
git log --oneline --graph --all
In a terminal, each commit line shows the names that point at it in brackets,
such as (HEAD -> photos) or (main). HEAD -> photos means HEAD points at
photos.
Detached HEAD
git switch expects a branch. To look at an older commit itself, ask for a
detached HEAD explicitly:
git switch --detach <commit>
git status then says HEAD detached at and the commit's short ID. You can
look around and even commit, but no branch moves. If you switch away, those
commits are left behind with no name, and Git warns you are leaving 1 commit behind. To keep them, create a branch while you are still there:
git switch -c <new-branch>
That branch starts at the current commit, so the work is safe. To leave a detached HEAD without keeping anything, switch back to a branch.
Older guides use git checkout <branch> to switch, git checkout -b <name> to
create and switch, and git checkout <commit> to detach. They still work;
git switch is the newer command that does only this job.
Exercise: Create, Switch and Inspect Branches
You will create branches, commit on them and watch which names move.
Starting state: Git configured as in the configuration lesson.
-
Run the setup. It makes two commits on
main:mkdir -p ~/git-practice/branch-basics cd ~/git-practice/branch-basics git init -b main cat > guide.md <<'EOF' # Trail guide EOF git add guide.md git commit -m "Add trail guide" cat >> guide.md <<'EOF' ## Safety Carry water. EOF git commit -am "Add safety tips" -
Look at the branches and the graph:
git branch git log --oneline --graph --allgit branchprints* main. The graph shows(HEAD -> main)on "Add safety tips". -
Create a branch, then look again:
git branch photos git branch git log --oneline --graph --allBoth branches are listed, but the
*is still onmain: creating a branch does not switch to it. The newest commit shows(HEAD -> main, photos), because both names point at it. -
Switch to the new branch:
git switch photos git branchGit prints
Switched to branch 'photos', and the*moves tophotos. -
Commit on
photosand see which name moves:cat > photos.md <<'EOF' # Photo spots The ridge at sunrise. EOF git add photos.md git commit -m "Add photo spots" git log --oneline --graph --allIllustrative output:
* bbac593 (HEAD -> photos) Add photo spots * 95b233f (main) Add safety tips * 7cfe2bc Add trail guidephotosmoved to the new commit, taking HEAD with it.mainstayed where it was. -
Switch to
mainand back, checking the files each time:git switch main ls git switch photos ls git switch mainphotos.mdis missing onmainand back onphotos. You end onmain. -
Create a second branch from
mainand switch to it in one step, then commit:git switch -c maps cat > maps.md <<'EOF' # Maps Download the park map before you go. EOF git add maps.md git commit -m "Add map links" git log --oneline --graph --allThe graph now forks. Each branch grew its own commit from "Add safety tips":
* fde0760 (HEAD -> maps) Add map links | * bbac593 (photos) Add photo spots |/ * 95b233f (main) Add safety tips * 7cfe2bc Add trail guide -
Try a detached HEAD, commit on it, then keep the work:
git switch --detach main git status cat > ideas.md <<'EOF' # Ideas A night hike? EOF git add ideas.md git commit -m "Try a night hike idea" git log --oneline --graph --all git switch -c ideas git log --oneline --graph --allgit statussaysHEAD detached atwithmain's commit ID. After the commit, the graph shows(HEAD)on "Try a night hike idea" with no branch name. Aftergit switch -c ideas, it shows(HEAD -> ideas): the commit now belongs to a branch.
Success: git log --oneline --graph --all shows main on "Add safety
tips", photos and maps each one commit ahead of it on separate lines, and
(HEAD -> ideas) on the night hike commit.
If something differs:
| What you see | Likely cause | Next step |
|---|---|---|
| No names in brackets in the graph | Output is not going straight to a terminal, or decoration is turned off | Add --decorate to the git log command |
fatal: a branch is expected, got commit | git switch was given a commit, not a branch | Add --detach, or switch to a branch name |
fatal: invalid reference: photo | A misspelled branch name | Check the spelling with git branch |
fatal: a branch named 'photos' already exists | The branch was created earlier | Switch to it with git switch photos |
Your local changes to the following files would be overwritten by checkout | Uncommitted edits to a file that differs on the other branch | Commit or discard them, then switch |
you are leaving 1 commit behind | You switched away from a detached HEAD with new commits | Run the git branch <new-branch-name> <ID> command Git printed |
-
Clean up:
cd ~/git-practice ls rm -rf branch-basics ls
Independent task: before cleaning up, switch to main, then create a
branch named glossary and switch to it with one command. Commit a new file
glossary.md on it. Before looking at the graph, predict which names will
point at your new commit and which commit main will point at. Then check.
Check Your Understanding
mainandfeatureboth point at commit B, and you are onfeature. You make commit C. Where domain,featureand HEAD point now?- You are on a detached HEAD at commit B and make commit C. Which reference
moves? What happens to C if you then switch to
main?
Answer and Reasoning
Independent task
git switch main
git switch -c glossary
cat > glossary.md <<'EOF'
# Glossary
Cairn: a pile of stones that marks a trail.
EOF
git add glossary.md
git commit -m "Add glossary"
git log --oneline --graph --all
The new commit shows (HEAD -> glossary): only glossary and HEAD point at
it. Its parent is "Add safety tips", where main still points, because the
branch was created from main and main does not move when you commit on
another branch.
Understanding check
featureand HEAD point at C, because HEAD points atfeatureand the current branch moves forward with each commit.mainstill points at B.- Only HEAD moves, to C; no branch records it. If you switch to
main, C is left with no name pointing at it, and Git warns that you are leaving a commit behind. To keep C, rungit switch -c <name>before switching, or thegit branchcommand Git prints afterwards.