

The “ -t” option in checkout stands for “ track” and it is used to create your branch and setting up the upstream branch automatically to the remote branch.Īs an example, let’s say that you have a branch named “remote-branch” on the “origin” remote. You can then switch to it by executing “git checkout” with the “-t” option and the name of the branch.

In order to switch to a remote branch, make sure to fetch your remote branch with “git fetch” first. In some cases, you may be interested in checking out remote branches from your distant repository. Switched to a new branch 'non-existing-branch'Ĭongratulations, you have now successfully switched to another branch and you can start working on it. To solve this error, make sure to append the “-c” option to the “git switch” command to specify that you want to switch to a new branch. On the other hand, if you try to switch to a non existing branch, you will get the following error message $ git switch non-existing-branchįatal: invalid reference: non-existing-branch In order to switch from the “master” branch to the “feature” branch, use the “git switch” command and specify the destination branch (which is “feature” in this case) $ git switch feature $ git switch Īgain, as an example, let’s say that you want to switch to the “ feature” branch from the “ master” branch. If the destination branch does not exist, you have to specify the “ -c” option (for “ create branch“), otherwise you will get an error message when switching to that branch.

Switch branch using git switchĪ quick way of switching branch on Git is to use the “ git switch” command and specify the name of the branch you want to switch to. Now that you know more about the “checkout” command, let’s see another useful command to change branch using Git. To solve this error, you will have to append the “-b” (for “new branch”) option to the checkout command. On the other hand, if you try to switch to a non-existing branch, you will the following error message $ git checkout non-existing-branchĮrror: pathspec 'non-existing-branch' did not match any file(s) known to git You have successfully switched to your “feature” branch with the checkout command.
