Git Switch Branch
When working on a project, it is good to work on separate branches to avoid corrupting the rest of the project. Git Switch is a command that helps us to create new branches and move from one branch to another. It is very similar to Git Checkout but has lesser functionality when compared to the Checkout command.
Branching in Git is a feature that allows developers to work in an independent space without worrying about affecting the rest of the project. Moving from one branch to another is also a common operation.
- Git Checkout is the most used command for this purpose and is also more popular than Git Switch.
- Git Switch is a slightly newer command and was added in Git version 2.23.
- While Git Checkout is capable of a lot more than just switching branches, the Git Switch command was added solely to switch between branches.
Git Switch Command
The Git Switch command is used to switch between branches. There are a few additional options we can use with it for added functionalities. Let's take a look at how to use this command.
To simply switch to an existing branch, we just need the name of that branch.
$ git switch <branch-name>
We can create a new branch and switch to it by using the -c or the --create.
$ git switch -c <new-branch-name>
The above command will create a new branch that is based on the HEAD. To create a branch based on some other commit point we can pass that commits position relative to our current HEAD. For example, the following command will create and switch to a branch that will be based on the n-th commit before the HEAD.
$ git switch -c <new-branch-name> HEAD~N
Git Switch provides us a quicker way to switch the previously checked-out branch(the branch we were on before switching to our current branch). We can do this by using -(hyphen) instead of entering the entire branch name.
$ git switch -
We can also forcefully reset an existing branch to a new start point by using the -C or the --force-create. One thing to note here is that we will be losing the commits on that branch. Consider the following illustration in which we have already have a feature branch with one commit. Now when we use the -C flag to reset the feature branch then that commit will be lost because we won't have any reference to it. To get this commit back we can use the -d option to detach our HEAD to that commit and then create another branch based on that commit. But we must know the commit hash to be able to do that.
$ git switch -C <branch-name>
We can use the --discard-changes option if we want to clear our working tree and staging area of any changes before switching to another branch. Git will by default try to block a switch if it causes a conflict in the files. The --discard-changes are used to bypass this security feature.
Git Switch can also take us to a previous commit point with the help of -d or --detach. This will be a Detached HEAD state.
$ git switch -d <commit-hash>
Creating and Switching between branches is something that we will often do when working on any project that is version controlled. Git Switch is a command that enables us to do all this. It is quite similar to the existing Git Checkout command but is sole dedicated to switching between branches whereas the Checkout command has a few additional features like undoing and discarding changes. Git Switch has also made it very easy to switch to the previously checked-out branch by just using the -(hyphen) instead of entering the entire branch name. We learned about a few additional options we can use with this command that makes working with branches a lot easier.