How to Switch Between Branches in Git
-
Use
git checkout
to Switch Between Branches in Git -
Use
git switch
to Switch Between Branches in Git -
Use
git checkout
With the-t
Option to Checkout a Remote Branch in Git
In this tutorial, we will learn how to switch between branches in Git.
Git, a distributed version control system, is a great tool for versioning in a collaborative development environment. In Git, we create repositories, and in the repositories, we create branches to track various development efforts.
We often create a new branch from the mainline for bug fixing or developing a new feature. Thus, as we have multiple branches in our Git repository, we often switch between branches.
We will now illustrate this with an example.
Use git checkout
to Switch Between Branches in Git
We have multiple team members or teams using the same project repository in Git in a collaborative development environment.
Git provides us the facility to create multiple branches in our repository. Thus, many teams or team members working on the same or different parts of the project would create branches for their use.
In Git, we can easily switch between branches created in the Git repository. One of the easiest ways to switch between branches is to use the git checkout
command.
The command’s syntax to switch to an existing branch is git checkout <existing_branch>
. Thus, suppose we have a branch named feature
in our repository, and we are now in the branch named main
.
We can list the branches using the git branch
command.
git branch
* main
feature
We would execute the git checkout
command to switch to the feature
branch.
$ git checkout feature
Switched to branch 'feature'
Suppose we want to switch to a non-existing branch, create a new branch, and switch to it.
We can do that using the git checkout
with the -b
option.
$ git checkout -b bug-fixes
Switched to a new branch 'bug-fixes'
Thus, we created a bug-fixes
branch and switched to it with the above command.
Use git switch
to Switch Between Branches in Git
Another way of switching between branches in Git is by using the git switch
command.
Thus, as with the git checkout
command, to switch to an existing branch viz. feature
, we would execute the git switch
command.
$ git switch feature
Switched to branch 'feature'
Also, if we want to create a new branch and then switch to it, we can do it using the git switch
command.
We must use the -c
option with the git switch
command.
$ git switch -c new-feature
Switched to a new branch 'new-feature'
Thus, we have now created a new branch viz. new-feature
and switched to it.
Use git checkout
With the -t
Option to Checkout a Remote Branch in Git
Sometimes, we may want to switch to the branches created by other team members. These branches would be in the remote repository, and we are interested in checking out these remote branches.
To check out the remote branch, the syntax of the git checkout
is git checkout -t <remote>/<branch>
.
Suppose we have a branch named release
in our remote repository, and we want to check out that branch.
First, we need to execute the git fetch
command to download objects and refs from the remote repository.
Then, we would execute the git checkout
command with the -t
option.
$ git fetch
$ git checkout -t origin/release
Branch 'release' set up to track remote branch 'release' from 'origin'.
Switched to a new branch 'release'
Thus, we have checked out the remote branch viz. release
in the remote repository given by the alias origin
. Please note that origin
is the default alias for the URL of our remote repository.
The -t
option given to the git checkout
is for tracking. It is used to create a branch and automatically set up the upstream branch to the remote branch.
Therefore, as shown above, the remote tracking information is set automatically. Thus, we have shown how to switch between branches in Git.
For more information, please visit these links.
Related Article - Git Checkout
- Git Checkout VS Pull
- Difference Between Git Checkout and Git Clone
- How to Checkout a Remote Git Branch
- How to Ignore Local File Changes in Git
- How to Rollback to an Old Commit in a Public Git Repository
- Difference Between Git Checkout --Track Origin/Branch and Git Checkout -B Branch Origin/Branch