Introduction
One of the advantages of working in Git is that it provides the ability to work on multiple project features simultaneously by keeping the code in separate branches. The branches allow users to focus on the part of the project that needs immediate attention and later get to work on something else.
In this tutorial, you will learn how to switch branches in Git and efficiently manage your project development.
Prerequisites
- Git installed (install Git on Ubuntu, macOS, Windows, CentOS 7, or CentOS 8).
- A Git repository.
How to Switch Branches in Git?
There are two ways to switch branches in Git:
git checkout
- used to switch between different branches or commits in a Git repository, allowing you to navigate and work on different project versions. The command also provides functionality for creating a new branch before switching when the-b
option is specified.git switch
- a Git command introduced in version 2.23 that allows users to switch between branches in a repository, making it a more intuitive alternative to thegit checkout
command. If the branch doesn't exist, specify the-c
option to create it before switching.
The sections below offer detailed instructions on using each command to navigate a Git repository efficiently.
1. How To Switch to Main Branch in Git?
In Git, the main branch typically refers to the default and often the primary long-lived branch in a Git repository.
Note: The naming convention for the main branch has evolved in recent years due to discussions about inclusivity and terminology. Previously, the main branch was commonly called master
, but the term is being replaced by the more neutral and inclusive term main
.
Switch to the main branch with git checkout
by running the following command:
git checkout main
To switch to the main branch using git switch
, run:
git switch main
Both commands switch from the current branch to the main branch in the Git repository. If your main branch is still using the term master
, specify that keyword in the command instead of main
.
Note: See how to merge the master branch into another one.
2. How Do I Switch to Another Branch in Git?
To switch to another branch in Git, specify the branch name after git checkout
or git switch
command. To see a list of all the available branches in a repository, run:
git branch
The command outputs all the available branches in the local repository. For example, to switch to the test
branch, run:
git checkout test
or
git switch test
The command switches from the current branch (new-feature
) to the specified test
branch.
It is also possible to use the -B
option with the git checkout
command to switch to another branch and to make the branch start from a specific commit.
The syntax is:
git checkout -B [branch] [start_point]
To find the exact starting point, list the commits in the repository using the git log
command. Use this command:
git log --oneline --graph
Choose the commit you want to start from and specify it in the command. For example, in the output above, the HEAD
of the master
branch is at 8666208, but we want to check out the commit just before HEAD
(231b946). The command we must run is:
git checkout -B master 231b946
The command switches to the master
branch and resets it to start from the specified commit.
3. How Do I Switch to a Remote Branch in Git?
If you want to switch to a remote branch, create a local tracking branch based on the remote branch and then switch to it. The git checkout
and git switch
commands allow you to switch to a remote branch in Git.
Note: See how to list the branches in a remote Git repository and then pull all branches.
However, before running either command, it is necessary to fetch the remote branch using the git fetch command. The command retrieves the latest changes from the remote repository and ensures you get the latest branch version. Follow the steps below:
1. Run the git fetch
command:
git fetch
2. Use the following syntax to switch to the remote branch:
git checkout -t [repository_name]/[branch_name]
or
git switch -t [repository_name]/[branch_name]
The -t
option instructs Git to create the branch and set it to track the upstream branch automatically. For example:
In the example above, we switch to the test-branch
branch and automatically set the remote tracking information. Consequently, any commits will be automatically pushed to the upstream branch.
Note: Check out our comparison article Git Switch vs. Checkout to learn more about switching branches.
What if Switching Branches in Git is Not Working?
The cause of git switch
or git checkout
not working when switching to a branch is usually because the branch does not exist yet. In that case, Git cannot switch to a non-existing branch. The error usually looks like this:
The solution to the problem is to use the -b
option with git checkout
or the -c
option with git switch
. These commands instruct Git to create the branch first and then switch to it. The syntaxes for both commands are:
git switch -c [branch-name]
and
git checkout -b [branch-name]
For example:
The command instructs Git to create a new branch and then switches to that branch.
Conclusion
This tutorial has shown how to switch branches in Git using the git checkout
and git switch
methods. Although there is a move to use git switch
more, git checkout
is still a powerful command used interchangeably with git switch
.
For more Git tutorials, see how to overwrite a local branch with a remote one or learn to compare two Git branches.