What's the difference between git switch and git checkout <branch>

Bastian Venthur picture Bastian Venthur · Jul 30, 2019 · Viewed 23.6k times · Source

Git 2.23 introduces a new command git switch -- after reading the docs, it seems pretty much the same as git checkout <branchname> can someone explain the difference or use case?

Two new commands "git switch" and "git restore" are introduced to split "checking out a branch to work on advancing its history" and "checking out paths out of the index and/or a tree-ish to work on advancing the current history" out of the single "git checkout" command.

Answer

kowsky picture kowsky · Jul 30, 2019

Well, according to the documentation you link to, its sole purpose is to split and clarify the two different uses of git checkout:

  • git switch can now be used to change branches, as git checkout <branchname> does
  • git restore can be used to reset files to certain revisions, as git checkout --<path_to_file> does

People are confused by these different ways to use git checkout, as you can see from the many questions regarding git checkout here on Stackoverflow. Git developers seem to have taken this into account.