The git switch branch or git checkout branch are a git CLI that used to move the cursor from branch to another branch during the stack.
Table Of Contents
The switch concept used to change the current branch to another branch which means select or move the cursor into another branch.
Anyway, in the following image, there is asterisk beside the branch name and the green highlighted color two both refers to the selected branch.
So once you select the branch using the following commands it would be highlighted as in the above image.
However, you will need to use the branch name when you would like to upload the changes into the remote repository.
Let’s see how to change the cursor position between the branches.
Select Another Branch using Git Switch CLI
The switch command
git switch used to move the cursor into another existing branch in the repository. The basic expression of switch would be like the below.
git switch [BRANCH-NAME]
In the following example I will chose another branch using switch command.
git switch design
It would show you the change in the CLI like the below.
Anyway, if I switched to non-existing branch, it would show you an error like below.
But if you need to switch into another non-existing branch using this command you have to use -c flag beside the command like the below.
git switch -c [BRANCH-NAME]
The -c flag means –create, so so it is doing two tasks which are: add and switch in the same time.
In the following section, will show you another way to add and switch to another branch.
Select a Git Branch using Git Checkout CLI
There is another way to select another existing branch in the stack by the
git checkout. It accepts a string beside the command which is the name of the branch.
git checkout [BRANCH-NAME]
Let’s see an example.
git checkout developer
But if the branch name doesn’t exist in the repository, it will show an error like in the below image.
In other hand, there is a way to add a branch if it doesn’t exist and move the current cursor into the new branch in the same time, you only need to add the -b flag beside the checkout command like the below example.
The -b flag refers to the branch word which will insert a new branch into the repository if it already doesn’t exist in the current repository.
git checkout -b team
Once you run this command, it will show you a message like the below.
Switched to a new branch 'team'
As you know, the git branch consists of many commits and refs which allow you go back as you need but what about if you need to move into another branch and add it at a specific point if it doesn’t exist using the
git checkout command?
I will answer this question in the following part.
Move into Another Branch at a Specific point
It is so easy to do that, by
git checkout command, but you have to pass more arguments in the CLI, Let’s see the main expression of the git checkout command to add a new branch at a specific point.
git checkout -B [BRANCH-NAME] [COMMIT-POINT]
But before you do that, you have to know what is the point number that you will use in the command to start the new branch from.
And that can be done using the below git command.
git log --oneline
Once you run this command, it will show you all refs and commits like in the below image.
The following step, you have to select the point to move into another branch at the same point, for example, I will use the “564771c” as a started point.
Let’s checkout a branch at this point by the following command.
git checkout -B codedtag 564771c
By this command, it will add a new branch into the point number “564771c“.
Anyway, these two commands, checkout and switch, are leading us to know what is the difference between them. So let’s move into the following section.
The Difference Between Switch and Checkout
Actually, the two both are changing the current branch and also creating a new branch if the branch name doesn’t exist. And that can be done using some additional flags.
But the switch command only created to do one thing, which changing the branch and adding a branch if it doesn’t exist by additional flag.
In other hand, the checkout command is an old git expression, designed to do a lot of things. So, it used checkout-index to copy the files and changes from another index to the current working tree without switching the current branch.
That is happening with a commit in the history. So it can restore the files in the working tree.
If you need to learn more about checkout you have to run the following command on your CLI.
git checkout --help
It will show you a web page located on your pc contains all information about the checkout command. check the below image.
Anyway, Let’s see how to move a file from a branch to another one using checkout.
In this article, you understood what git checkout is and how to switch between git branches by using the git switch branch cli.
To see more tutorials, visit Git tutorials page