Lesson 2 of 9 · about 15 minutes
Create, switch, compare
Five commands that let you move between branches without fear.
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:
| Command | What it does |
|---|---|
git switch -c add-cleaning | Create a branch at your current commit and move HEAD onto it.2 |
git switch main | Move 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 -vv | List local branches with their latest commit. The * marks where HEAD is.3 |
git log --oneline --graph --all | Draw 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.
-
$ gh repo create penguins-lab --public --clone --add-readme $ cd penguins-lab -
$ git switch -c add-cleaning Switched to a new branch 'add-cleaning' -
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) -
$ 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(+) -
$ git branch -vv * add-cleaning 3ef57a2 Add drop_missing cleaning function main 1d28f0e [origin/main] Initial commit $ git status On branch add-cleaningYour hashes will differ.
mainshows[origin/main]because it is linked to GitHub. Lesson 4 explains that link. -
$ git switch main $ ls README.md $ git switch - $ ls README.md clean.pyNothing was deleted.
main's commit simply doesn't containclean.py, and switching makes your files match the branch's commit.2 -
$ git log --oneline --graph --all * 3ef57a2 (HEAD -> add-cleaning) Add drop_missing cleaning function * 1d28f0e (origin/main, origin/HEAD, main) Initial commitSame picture as lesson 1:
add-cleaningis one commit ahead ofmain. -
$ 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 frommain. 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.
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?
- Still on the previous branch
- On the new add-cleaning branch
- On no branch at all
- On a detached HEAD commit
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?
- main's commit has no clean.py
- Git deleted the file permanently
- The branch was merged away
- GitHub removed the file remotely
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?
- Only changes made on add-cleaning
- Only changes made on main
- Every difference between both tips
- Commits that exist on GitHub
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.
HEAD moves, with diagrams. Then skim the git switch reference to see what else the command can do.
git diff main..add-cleaning and git diff main...add-cleaning with a diagram." Lesson 10 answers that second question in full.
Sources
- Chacon & Straub, Pro Git, §3.1 Switching Branches.
- Git reference, git-switch.
- Git reference, git-branch (-v, -vv).
- Git reference, git-log (--oneline, --graph, --all).
- Git 2.51.0 release notes.
- Git reference, git-diff (A...B).
- GitHub Docs, Three-dot and two-dot Git diff comparisons.