DEV Community 👩‍💻👨‍💻


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.

Top comments (0)

Thank you.

Thanks for visiting DEV, we’ve worked really hard to cultivate this great community and would love to have you join us. If you’d like to create an account, you can sign up here.