Lesson 2 of 9 · about 15 minutes

Create, switch, compare

Five commands that let you move between branches without fear.

You will make a real repository, put new work on a feature branch, hop back and forth between it and main, and see exactly what the branch changed.

The five commands

Lesson 1 happened in a simulator. This time you'll use your own terminal: Git Bash on Windows, or Terminal on macOS/Linux. Here is everything you need:

CommandWhat it does
git switch -c add-cleaningCreate a branch at your current commit and move HEAD onto it.2
git switch mainMove to an existing branch. Your working tree is rewritten to match it.2
git switch -Jump back to the branch you were on before.2
git branch -vvList local branches with their latest commit. The * marks where HEAD is.3
git log --oneline --graph --allDraw every branch's history as a text graph.4

The docs describe git switch -c as "the transactional equivalent of git branch <new-branch> and git switch <new-branch>". It is lesson 1's two steps in one.2

Older tutorials (and Claude, sometimes) use git checkout -b add-cleaning. It does the same thing. git switch arrived in Git 2.23.1 It was labelled experimental until Git 2.51 declared it "no longer experimental."5 This course uses switch because it does one job: changing branches.

Do it: your practice repository

You'll keep using penguins-lab for the rest of the course, so set it up properly. Tick each step as you go. Your progress is saved in this browser.

  1. $ gh repo create penguins-lab --public --clone --add-readme
    $ cd penguins-lab
  2. $ git switch -c add-cleaning
    Switched to a new branch 'add-cleaning'
  3. import pandas as pd
    
    def drop_missing(df: pd.DataFrame, cols: list[str]) -> pd.DataFrame:
        """Return a copy of df without rows that have NaN in any of cols."""
        return df.dropna(subset=cols).reset_index(drop=True)
  4. $ git add clean.py
    $ git commit -m "Add drop_missing cleaning function"
    [add-cleaning 3ef57a2] Add drop_missing cleaning function
     1 file changed, 5 insertions(+)
  5. $ git branch -vv
    * add-cleaning 3ef57a2 Add drop_missing cleaning function
      main         1d28f0e [origin/main] Initial commit
    $ git status
    On branch add-cleaning

    Your hashes will differ. main shows [origin/main] because it is linked to GitHub. Lesson 4 explains that link.

  6. $ git switch main
    $ ls
    README.md
    $ git switch -
    $ ls
    README.md  clean.py

    Nothing was deleted. main's commit simply doesn't contain clean.py, and switching makes your files match the branch's commit.2

  7. $ git log --oneline --graph --all
    * 3ef57a2 (HEAD -> add-cleaning) Add drop_missing cleaning function
    * 1d28f0e (origin/main, origin/HEAD, main) Initial commit

    Same picture as lesson 1: add-cleaning is one commit ahead of main.

  8. $ git diff main...add-cleaning
    diff --git a/clean.py b/clean.py
    new file mode 100644
    --- /dev/null
    +++ b/clean.py
    @@ -0,0 +1,5 @@
    +import pandas as pd
    ...

    main...add-cleaning (three dots) compares the branch with the commit where it split from main. You see the branch's own changes and nothing else.6 GitHub pull requests use the same three-dot view.7 Later, this is how you'll review everything an agent did on its branch.

Leave add-cleaning unmerged. Lesson 3 brings it into main.

You can switch with uncommitted edits. Git carries them along to the other branch, which surprises people. If switching would overwrite your changes, Git refuses: "The operation is aborted however if the operation leads to loss of local changes."2 The simple habit is to commit before you switch.

On Windows you may see LF will be replaced by CRLF warnings. They are about line endings, and you can ignore them for this course.

Recall the commands

Type each command from memory. Don't scroll up. The effort of remembering is the point.

Create a branch called add-cleaning and move onto it, in one command.

Move to the existing main branch.

Jump back to whichever branch you were on before.

Show the history of every branch as a one-line-per-commit graph.

Show only the changes made on add-cleaning since it split from main.

Check yourself

You run git branch add-cleaning (no switch). Where is HEAD now?

git branch <name> only creates the pointer. Pro Git: it "only created a new branch — it didn't switch to that branch."

After git switch main, clean.py disappears from your folder. Why?

Switching updates the working tree to match the branch's commit. The file is safe in the add-cleaning commit.

What does git diff main...add-cleaning (three dots) show?

Three dots diff the branch against its merge base with main, so changes that landed on main later don't clutter the view. That's exactly what a pull request shows.

Read the "Switching Branches" part of Pro Git §3.1. It shows the working tree changing as HEAD moves, with diagrams. Then skim the git switch reference to see what else the command can do.
Ask Claude: "I have uncommitted changes and want to switch branches. What are my options?" or "Explain the difference between git diff main..add-cleaning and git diff main...add-cleaning with a diagram." Lesson 10 answers that second question in full.

Sources

  1. Chacon & Straub, Pro Git, §3.1 Switching Branches.
  2. Git reference, git-switch.
  3. Git reference, git-branch (-v, -vv).
  4. Git reference, git-log (--oneline, --graph, --all).
  5. Git 2.51.0 release notes.
  6. Git reference, git-diff (A...B).
  7. GitHub Docs, Three-dot and two-dot Git diff comparisons.