With Git, there are often many different ways to do the same thing. A lot of your work with Git will involve the formation of habits, but there may be occasions in which you will adapt new commands to improve your workflow. The Git
switch command is relatively new, but it works as a worthy alternative to the
checkout command when you need to rapidly switch between different branches or create new branches. This procedure goes along well with creating your own Git server on unmetered VPS (and other VPS) hosting is not what you think.
For some new users, remembering the
switch command may be an easier way to learn about managing branches. The
checkout command supports many of the same actions, but it will also does a lot more, so substituting in
switch for branch management can potentially simplify your workflow — and reserve
checkout for more specific needs.
For developers or sysadmins experienced with the command line, get high availability and root access for your application, service, and websites with Cloud VPS Hosting.
Quick Branch Switching
The most basic example of
switch is as simple as naming a branch:
git switch <branch name>
To save yourself some extra typing, you can switch back to the previous branch you were working on with a simple
git switch -
But, as with the
checkout command, you will want to make sure your working state is clean before attempting a switch. If you have uncommitted changes in your working directory on the current branch you will receive a warning if attempting a switch:
error: Your local changes to the following files would be overwritten by checkout: new-file.txt Please commit your changes or stash them before you switch branches. Aborting
At this point, you can stash your changes if you’re not ready to commit them. Or, go ahead and commit these changes before running your
git switch. But again, you have other options.
For example, imagine you made changes and don’t want to stash or commit them. Rather, you would just like to forget these changes and move on. You can use the
--discard-changes option to switch without preserving your changes:
git switch <alternate branch> --discard-changes
Creating Branches With a Git Switch
switch you can also create new branches. This works similarly to the
git switch -c <name of new branch>
Instant Remote Tracking
switch command also saves some time when tracking remote branches. You may recall in the … we set up a new branch, pushed it to the remote, and then set it as an upstream branch for itself.
You can skip a few of these steps if there is already a remote branch out there that you want to bring it into your local configuration:
git switch <name of remote branch>
This command not only creates the remote branch in your local workstation but also switches to it and sets it up for tracking, which means you can run a
git push or
git pull without having to specify the branch.
Well done! You now know how to quickly switch between Git branches using the
switch command. If you have any questions about this procedure feel free to drop them below.