It’s Finally Time to Say Goodbye to “git checkout”
“Git switch” and “git restore” are here to stay
Git is the most widely used version control system for developers. One of the most commonly used commands with Git is git checkout, which allows users to switch between branches and restore files to a previous moment.
In 2019 however, with the release of Git 2.23, two new commands were introduced to replace git checkout for a more intuitive and streamlined workflow: git switch and git restore. Despite its release almost 4 years ago, developers have been slow to let go of using git checkout (old habits die hard I guess).
In this article, we are going to take a look at which disadvantages of git checkout the Git team tried to solve by introducing git switch and git restore, and why that should lead to no longer usage of git checkout.
The problem with git checkout
git checkout is a command with two core functionalities:
- switching between branches
- restoring files to a previous state
However, these two functionalities are not clearly differentiated in their command syntax, which can lead to confusion and mistakes. For example, if you accidentally type git checkout <commit> instead of git checkout <branch>, you’ll end up in a so-called “detached HEAD” state, which means that any new commits you make will not e associated with any branch. This can particularly be problematic when switching to a new branch after you’ve made changes in the detached HEAD without committing them, as those changes will be lost, and there is no way to recover them.
Introducing git switch and git restore
In an attempt to mitigate these issues with git checkout, the Git team introduced two new commands in Git version 2.23: git switch and git restore. These commands split the previously mentioned two core functionalities of git checkout into two separate commands.
git switch can solely be used for switching between branches. The syntax is straightforward: git switch <branch>. If you try to use git switch to switch to a commit, Git will throw an error rather than putting you in a detached HEAD state. Therefore it’s less likely that you can make commits unassociated to any branch.
git restore, on the other hand, can solely be used to restore a file to a previous state. Its syntax is straightforward too: git restore <file>. If you accidentally try to switch to a branch instead of a file, Git will throw an error.
Benefits of using git switch and git restore
One of the key benefits of git switch and git restore is the improved safety. As the two functionalities are separated into two different commands, the chances that you unintentionally apply the wrong functionality decreases. This makes mistakes and frustration because you’ve lost work less likely to occur.
Another benefit of git switch and git restore is that its syntax is more straightforward because you don’t have to remember the different applications of git checkout. Even after years of using Git, many developers still fail to fully understand the workings and syntax of git checkout.
Also, the commands of git switch and git restore are more intuitive and precise. When you want to create a new branch, you can use git switch -c <branch> with the -c flag standing for create. When using checkout, you have to use git checkout -b <branch> with the -b flag standing for branch. However, you can also use git checkout <branch> to switch between branches too, so the -b flag is solely about creating a new branch, but a flag named branch isn’t very helpful to indicate that it’s about creating rather than switching to an existing one. Hence, the commands of git switch and git restore are more intuitive, and should therefore be preferred.
To conclude
While git checkout has been the go-to command for Git users for many years, with the introduction of git switch and git checkout there have been better alternatives out for a few years now. Having the two core functionalities of git checkout split into two separate commands, Git has made it easier to switch between branches and restore files without accidentally using the wrong command. If you’re still using git checkout rather than git switch and git restore, now is the perfect time to make the switch!






