What's the difference between git switch and git checkout <branch>
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, asgit checkout <branchname>
doesgit restore
can be used to reset files to certain revisions, asgit 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.
git checkout
is a bit of a swiss army knife in that has several unrelated uses.
If you modify a file but haven't staged the change, then git checkout <filename>
will reverse the modifications... a quick and easy way to cancel changes to a file. You remain in the same branch.
git checkout <branchname>
(as you noted) switches branches.
Two completely different purposes, which could lead to confusion if a file name and a branch name are similar.
Having it as two commands is clearer.
switch
has some limitations: at the moment you can switch from any commit to <branch name>
, however it's impossible to switch from <branch name>
to a particular commit with a status of detached HEAD. So you need to use git checkout 5efb
(where 5efb is an example of a hash reference to arbitrary commit)