Switch Current Branch (1) – Git Checkout
The git checkout
command is used for switching your current branch to a selected branch. You also create a new branch with the -b
option and immediately switch to the new branch. When you switch your current branch with changes in INDEX and/or the Working Tree, you need to carefully manage the operation as the changes may cause a problem.
Switch the current branch
As explained on the previous page, the git branch [new branch name]
creates a new branch but the current branch location stays the same. git checkout
is used when you want to switch your branch to a selected branch.
Example
To switch to Branch_A, run the command below.
git checkout Branch_A
Switched to branch 'Branch_A'
The image below illustrates an example of how the command above works.
Create a new branch and switch to the new branch
When you use this command with the -b
option, you can create a new branch and switch to the new branch at the same time.
Example
To create a new branch named Branch_A and switch to the new branch at the same time, run the command below.
git checkout -b Branch_A
Switched to a new branch 'Branch_A'
The image below illustrates an example of how the command above works.
Checkout when you have changes in INDEX and/or the working tree
When you checkout to another branch, the Git system tries to carry the changes in the Working Tree and INDEX over to the destination branch. If the latest commit statuses of both branches are the same, you can carry over the Working Tree and INDEX (Situation A).
Subscribe now for
uninterrupted access.