Skip to content

Creating and Switching Branches

Creating and switching branches are the two most common branch operations. Once you have a branch, you switch to it, make commits, and those commits belong to that branch — main and every other branch stay exactly where they were.

Terminal window
git branch feature/tour-descriptions

This creates a new branch pointing at your current commit. It does not switch you to the new branch — you stay on whatever branch you are currently on. To confirm:

Terminal window
git branch

Output:

feature/tour-descriptions
* main

The * marks your current branch. You are still on main. The new branch exists, but HEAD has not moved.

Terminal window
git switch feature/tour-descriptions

Output:

Switched to branch 'feature/tour-descriptions'

Now HEAD points at feature/tour-descriptions. Any commits you make will advance this branch, not main.

Run git log --oneline --graph --all to see both branches pointing at the same commit:

* b7e9d03 (HEAD -> feature/tour-descriptions, main) Add difficulty and gear notes
* a3f8c12 Add initial tour notes

The most common workflow combines both steps:

Terminal window
git switch -c feature/tour-descriptions

-c means “create.” This is the command you will use almost every time — create the branch and switch to it immediately.

On feature/tour-descriptions, add content to a file:

Terminal window
echo "Best views from the ridge at mile 3" >> pine-ridge.txt
git add pine-ridge.txt
git commit -m "Add viewpoint note to Pine Ridge description"

Now check the graph:

Terminal window
git log --oneline --graph --all
* c2a1d08 (HEAD -> feature/tour-descriptions) Add viewpoint note to Pine Ridge description
* b7e9d03 (main) Add difficulty and gear notes
* a3f8c12 Add initial tour notes

feature/tour-descriptions has moved forward with the new commit. main is still at b7e9d03. The branches have diverged.

Terminal window
git switch main

When you switch branches, Git updates your working tree to match the state of the branch you switched to. Open pine-ridge.txt — the line you added on the feature branch is gone. It is not deleted; it lives on feature/tour-descriptions. Switch back and it reappears.

This is one of the things that surprises people the first time: your files actually change when you switch branches. Git is updating your working tree to reflect the state of the commit that branch points to.

What switching requires: a clean working tree

Section titled “What switching requires: a clean working tree”

Git will not let you switch branches if you have uncommitted changes that would be overwritten by the switch. If you have modified files, Git will warn you:

error: Your local changes to the following files would be overwritten by checkout:
pine-ridge.txt
Please commit your changes or stash them before you switch branches.
Aborting

Your options:

  1. Commit the changes on the current branch before switching
  2. Stash them temporarily (git stash, covered in Module 04)

This protection prevents you from accidentally losing work.

Terminal window
git branch # local branches only
git branch -a # local and remote branches

The * marks your current branch.

Before git switch existed (added in Git 2.23, 2019), the command was git checkout:

Terminal window
git checkout feature/tour-descriptions # switch
git checkout -b feature/tour-descriptions # create and switch

Both still work. You will see checkout in older documentation, Stack Overflow answers, and tutorials. git switch is the modern preferred form for branch operations.

  1. In your git-practice folder, create a new branch and switch to it in one step:
Terminal window
git switch -c feature/add-river-canyon
  1. Add content to river-canyon.txt and make a commit on this branch:
Terminal window
echo "River Canyon Trail: canyon rim views and waterfall stops" > river-canyon.txt
git add river-canyon.txt
git commit -m "Add River Canyon trail description"
  1. Run git log --oneline --graph --all and observe that feature/add-river-canyon has advanced while main has not.

  2. Switch back to main:

Terminal window
git switch main
  1. Open your working tree and confirm that river-canyon.txt has no content (or does not exist) — the feature branch work is not visible from main.

  2. Run git log --oneline --graph --all again from main and confirm both branches are visible.

  • git branch <name> creates a branch at the current commit without switching.
  • git switch <name> switches to an existing branch.
  • git switch -c <name> creates and switches to a new branch in one step — the form you will use most.
  • Switching branches updates your working tree to reflect the state of that branch’s latest commit.
  • Git will not let you switch with uncommitted changes that would be overwritten.
  • git branch lists local branches; git branch -a includes remotes.

Next: merging — how to bring the commits from a feature branch back into main.