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.
Difference between git checkout and git switch It can also be used to restore changes from a certain commit. But git checkout does more than that. It allows you to copy files from any branch or commit directly into your working tree without switching branches.
The git checkout command lets you navigate between the branches created by git branch . Checking out a branch updates the files in the working directory to match the version stored in that branch, and it tells Git to record all new commits on that branch.
Difference between git checkout -- and git checkout Note that git checkout <name> is really meant for branches, but Git syntax is relaxed, and if Git can't find a branch, then it will look for a file.
The easiest way to switch branch on Git is to use the “git checkout” command and specify the name of the branch you want to switch to. If the destination branch does not exist, you have to append the “-b” option, otherwise you won't be able to switch to that 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, as git checkout <branchname>
doesgit restore
can be used to reset files to certain revisions, as git checkout -- <path_to_file>
doesPeople 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.
The switch
command indeed does the same thing as checkout
, but only for those usages that switch branches. In particular, unlike checkout
it cannot restore working tree files — that is instead done using the restore
command that was introduced alongside switch
.
As you noted in the 2.23.0 release notes section you quoted, the switch
and restore
commands were introduced to split the checkout
command into two separate pieces:
In other words, checkout
does two different things, and this release split each of those different things into its own focused command.
This dual purpose of checkout
can be seen in its summary description in the documentation:
git-checkout - Switch branches or restore working tree files
The commit that added the switch
command explains the rationale for the new commands in its commit message:
"git checkout" doing too many things is a source of confusion for many users (and it even bites old timers sometimes). To remedy that, the command will be split into two new ones: switch and restore. The good old "git checkout" command is still here and will be until all (or most of users) are sick of it.
From this, it's clear that the new commands were introduced to reduce confusion by having two focused commands, rather than one multi-purpose command.
Note that as of December 2021, the new commands are still listed as experimental (switch
, restore
):
THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.
I have not found a full comparison of the commands anywhere. From reading through the documentation, I think this should be a fairly complete comparison:
previous command | new command |
---|---|
git checkout <branch> |
git switch <branch> |
git checkout |
N/A (use git status ) |
git checkout -b <new_branch> [<start_point>] |
git switch -c <new-branch> [<start-point>] |
git checkout -B <new_branch> [<start_point>] |
git switch -C <new-branch> [<start-point>] |
git checkout --orphan <new_branch> |
git switch --orphan <new-branch> |
git checkout --orphan <new_branch> <start_point> |
N/A (use git switch <start-point> then git switch --orphan <new-branch> ) |
git checkout [--detach] <commit> |
git switch --detach <commit> |
git checkout --detach [<branch>] |
git switch --detach [<branch>] |
git checkout [--] <pathspec>… |
git restore [--] <pathspec>… |
git checkout --pathspec-from-file=<file> |
git restore --pathspec-from-file=<file> |
git checkout <tree-ish> [--] <pathspec>… |
git restore -s <tree> [--] <pathspec>… |
git checkout <tree-ish> --pathspec-from-file=<file> |
git restore -s <tree> --pathspec-from-file=<file> |
git checkout -p [<tree-ish>] [--] [<pathspec>…] |
git restore -p [-s <tree>] [--] [<pathspec>…] |
As shown in this comparison, some prior usages can be converted to the new commands by replacing the old command name (checkout
) to the new one (switch
, restore
), whereas others require additional adjustment. Notable changes include:
-b
/-B
options for creating a new branch before switching are renamed to -c
/-C
--detach
is now always required when switching to a detached head, where it was previously optional for commits but required for branches-s
option, rather than being an inline argumentswitch
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With