DEV Community

Swislok-Dev
Swislok-Dev

Posted on

Checkout or switch?

Switching branches with Git is simple with git checkout <branchname>. So what are so what are use cases for git switch instead?

As of Git version 2.23, git switch along with git restore, are here to make use of checkout in non-related ways in order to achieve the same purpose. While git checkout is able to perform tasks that involve such as running down to a specific commit on a detached head, switch cannot without specifying the --detached flag: git switch --detached <commit-or-branchname>.

With git restore, many of the same functions can be performed in a similar fashion with flags for given tasks the same as git checkout would have done.

The main purpose for switch and restore to have been created in the first place was to simplify switching to a branch and for restoring/undoing a commit.

According to the source documentation, this is an experimental feature and could be possible that some of it is changed.

Which command should be used? Ideally switch/restore should be used as they perform the same functions as checkout would. Perhaps it depends on what the individual has in mind or what they have gotten used to. Either way, at this time git checkout is not slated for deprecation, and there is no warning noting such an action is coming for checkout.

Both switch and checkout will do the same thing, with switch having fewer related options than what checkout will have access to.

Oldest comments (0)