Skip to content

Git checkout, switch and restore - which does what?

A few words on git checkout. It is one of the first commands anyone starting with Git learns, and also one of the most confusing - because it does two completely different things: it switches branches and it restores files. Since version 2.23 (2019), Git has separate commands for these, git switch and git restore. The old checkout still works and is not going anywhere, but it is worth knowing which is which.

In short

  • git switch branch switches branches, git switch -c new creates a new one.
  • git restore file discards uncommitted changes in a file, git restore --staged file unstages it.
  • git checkout branch -- file or git restore --source branch file takes a file from another branch.

One command, two jobs

If you are just starting, read Git in 15 minutes first. Here I assume you know what a commit and a branch are.

The problem with checkout shows in two commands that look almost the same:

git checkout new-look       # switches to the new-look branch
git checkout -- style.css   # throws away your changes to style.css

The first is safe. The second deletes unsaved work in the file without asking. And what if a branch and a file share a name? That is what the -- is for: everything after it is a file, not a branch. This confusion was the main reason the two new commands were added.

Switching branches: git switch

git switch new-look         # go to an existing branch
git switch -c menu-fix      # create a new branch and go to it
git switch -                # back to the previous branch

The same in the old syntax: git checkout new-look and git checkout -b menu-fix. git switch never touches a single file, so you cannot wipe your changes with it by accident.

What about uncommitted changes when you switch? If they do not clash with the target branch, Git simply carries them over - you will see them on the new branch as modified files. If they clash, it refuses and asks you to commit or stash them (git stash).

Restoring files: git restore

git restore style.css             # discard uncommitted changes in a file
git restore --staged style.css    # unstage a file, keeping the changes
git restore .                     # discard changes in every file

The old equivalents are git checkout -- style.css and git reset HEAD style.css. The new names say plainly what will happen. Just remember that changes discarded this way are in no commit, so Git cannot give them back. More on undoing things at different stages in Git - undoing changes.

A file from another branch or commit

This is what I once jotted down in a note: git checkout <name> -- <file/pattern>. It comes in handy more often than you would think. You are on main, and the new-look branch has a finished style.css you want right now, without merging the whole branch:

git checkout new-look -- style.css

Or with the new command:

git restore --source new-look style.css

There is one difference between them, which I checked: checkout stages the file straight away (ready to commit), while restore only changes the file on disk. To make restore behave like checkout, add --staged --worktree.

Instead of a branch you can give a commit hash or HEAD~3 (three commits back), and instead of a file a whole folder:

git restore --source HEAD~3 css/

"Detached HEAD": a walk through history

git checkout 3f2a1b7

This takes you to a specific commit, but not onto a branch. Git warns you about a detached HEAD: you can look around, run the project, even make commits, but they belong to no branch and are easy to lose once you go back. The new syntax wants an explicit flag here, so nobody ends up in this state by accident:

git switch --detach 3f2a1b7

If you make something in this state that you want to keep, create a branch straight away: git switch -c rescue.

Cheat sheet

  • go to a branch: git switch branch (old: git checkout branch),
  • new branch: git switch -c branch (old: git checkout -b branch),
  • previous branch: git switch -,
  • discard changes in a file: git restore file (old: git checkout -- file),
  • unstage: git restore --staged file (old: git reset HEAD file),
  • a file from another branch: git restore --source branch file (old: git checkout branch -- file),
  • look at an old commit: git switch --detach hash (old: git checkout hash).

A fuller list of commands is in the Git cheat sheet, and what working with branches looks like day to day is in Everyday work with Git.

Advertisement