


Git Switch is a command that enables us to do all this. $ git switch -d SummaryĬreating and Switching between branches is something that we will often do when working on any project that is version controlled. Git Switch can also take us to a previous commit point with the help of -d or -detach. The -discard-changes are used to bypass this security feature. Git will by default try to block a switch if it causes a conflict in the files. 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. But we must know the commit hash to be able to do that. 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. 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. Consider the following illustration in which we have already have a feature branch with one commit.

One thing to note here is that we will be losing the commits on that branch. We can also forcefully reset an existing branch to a new start point by using the -C or the -force-create. We can do this by using -(hyphen) instead of entering the entire branch name. 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). For example, the following command will create and switch to a branch that will be based on the n-th commit before the HEAD. To create a branch based on some other commit point we can pass that commits position relative to our current HEAD. The above command will create a new branch that is based on the HEAD. We can create a new branch and switch to it by using the -c or the -create. To simply switch to an existing branch, we just need the name of that branch.
#Git change branch how to#
Let's take a look at how to use this command. There are a few additional options we can use with it for added functionalities. The Git Switch command is used to switch between branches.

