How to Switch Branch in Git: A Complete Guide for Beginners
Last Updated :
05 Apr, 2025
Git is a Powerful tool that helps developers keep track of changes in their code. One of the important tasks in Git is Switching between branches. Branches allow you to work on different features, bug fixes, or experiments at the same time, without affecting the main project.
In this guide, you will learn how to easily switch branches in Git.
What is a Git Branch?
In Git, a branch is like a separate workspace where you can make changes without affecting the main project. Imagine you're working on a new feature or fixing a bug, and you don’t want it to mess up the main code. By creating a branch, you can make all your changes in isolation and then merge them back into the main branch when you're ready.
The main branch is usually called main or master and is the default branch in any Git repository.
Why Use Branches in Git?
- Work on New Features: You can work on new features without disturbing the main code.
- Fix Bugs: You can fix bugs without messing up other parts of the code.
- Experiment: You can try new ideas without affecting the main project.
How to Switch Between Branches in Git: 2 Easy Method
Switching between branches is simple, and you can do it using two commands: git checkout and the newer git switch. Let’s look at both of these.
Method 1: Using git checkout Command
The git checkout command is a versatile tool for navigating between branches in Git. It allows developers to switch to a different branch and update the working directory to reflect the state of the selected branch.
Syntax:
git checkout <branch_name>
Example: If you're working on a branch called feature-login
, but you need to switch to the main branch, you can use these commands:
Step 1: First, list all branches to see which ones are available
git branch

Step 2: Switch to the main branch
git checkout main

Git will now change your working directory to reflect the state of the main branch.
Method 2: Using git switch Command (Introduced in Git 2.23+)
The git switch command is a newer addition to Git, designed to provide a more intuitive and consistent experience for branch switching. It simplifies the syntax and offers clearer feedback during the process.
Syntax:
git switch <branch_name>
Example: Imagine you're working on a bug fix branch named "bug-fix" and want to switch to a release branch named "release-v1.0". Here's how you would use git switch:
Step 1: List available branches
git branch

Step 2: Switch to the feature-new branch
git switch feature-new

Using git switch is simpler because it focuses just on switching branches and provides clear feedback.
How to Create and Switch to a New Branch in Git
Sometimes, you need to create a new branch and switch to it right away. Follow the below steps to Create and Switch to a new branch in Git:
1. With git checkout:
git checkout -b <new branch_name>
For example, to create a new branch called feature-signup
, you would use:
git checkout -b feature-signup
2. With git-switch:
git switch -c <new branch name>
To Create and switch to the feature-login branch use:
git switch -c feature-login
Common Issues When Switching Branches
Uncommitted Changes: If you have uncommitted changes, Git won’t let you switch branches. You can either commit or stash your changes before switching.
How to Handle Uncommitted Changes Before Switching Branches
Before switching branches, Git prevents you from doing so if you have uncommitted changes. This is to avoid losing work or causing conflicts.
To switch branches safely, you have two options:
1. Commit the changes: If you’re done with your changes, commit them:
git add .
git commit -m "Your commit message"
2. Stash the changes: If you want to save your changes temporarily and switch branches without committing them, use git stash
:
git stash
git switch <other_branch_name>
git stash pop #To apply your stashed changes
Switching to a Remote Branch
To switch to a remote branch, Git first needs to know about it. You can do this using git fetch.
Step 1: Fetch the latest branches:
git fetch origin
This command updates your local reference of the remote repository but does not modify your working directory.
Step 2: Switch to the remote branch
If the branch is already tracked, simply use:
git switch <remote_branch_name>
Alternatively, if you need to create a local branch from a remote one:
git checkout -b <local_branch_name> origin/<remote_branch_name>
or using the git switch command:
git switch -c <local_branch_name> origin/<remote_branch_name>
When you check out a commit directly (instead of a branch), Git enters a "detached HEAD" state. In this state, you're not working on a branch, and any commits you make won’t belong to any branch.
To avoid this, always ensure you’re switching to a branch:
git switch <branch_name>
How to Handle Merge Conflicts When Switching Branches
When switching branches, you might encounter merge conflicts. These occur if changes on the branch you're switching to conflict with your uncommitted changes.
To resolve a merge conflict:
- Git will mark the conflicting files.
- Open the files, and manually choose which changes to keep.
- Once resolved, add the changes and commit:
git add <file_name>
git commit -m "Resolved merge conflict"
Conclusion
Switching branches in Git is a key skill for managing code changes. Whether you're using the classic git checkout
command or the newer git switch
, learning how to switch branches will help you work more efficiently on your projects. By isolating different features and fixes in their own branches, you can collaborate with other developers without worrying about breaking the main codebase.
Similar Reads
How to Create a Branch In Git from Another Branch?
Branching is a fundamental aspect of version control systems like Git, which helps developers work on multiple features or bug fixes simultaneously without interfering with the main codebase. Creating a branch from another branch is a common practice, especially when you want to build upon existing
3 min read
How To Checkout A File From Another Branch in Git?
Git is a powerful version control system that allows developers to manage code changes and collaborate effectively. One of its useful features is the ability to check out specific files from different branches, enabling you to incorporate changes or retrieve older versions without merging entire bra
4 min read
How to Create a New Branch in Git?
Git is a powerful and widely used version control system that helps developers manage code changes across projects efficiently. One of the fundamental features of Git is branching, which allows developers to diverge from the main line of development and work on different tasks or features independen
4 min read
How to Create a New Branch in Git and Push the Code?
Branching in Git is a helpful feature for software developers working on a big team project. It allows the team members to work on different aspects of the software by creating a branch from the main branch. The main branch is not affected by the changes in the created branch until it is merged into
8 min read
How to Delete a Branch in Git?
When working with Git, itâs common to create branches to isolate changes and work on new features or fixes. However, once a branch has served its purpose, itâs often necessary to delete it to keep your repository clean and organized. In this article, weâll see the process of deleting a Git branch us
3 min read
How To Create And Commit A Branch in GitLab?
GitLab is a popular Git repository hosting service with built-in tools for Continuous Integration/Continuous Deployment (CI/CD), issue tracking, and more. Branching is a fundamental aspect of Git workflow, allowing developers to isolate features, bugs, or experiments from the main codebase. This gui
3 min read
How To Get Changes From Master Into a Branch in Git?
In Git, branches are essential for organizing and managing development work. Whether you are working on a feature, a bug fix, or any other task, you often work in a separate branch so that the changes donât directly affect the master or main branch (the primary production branch). There are two main
2 min read
How To Copy A Version of Single File From One Git Branch To Another?
Working with branches in Git is a common practice in version control systems to manage multiple lines of development. Sometimes, you might want to copy a single file's version from one branch to another, whether itâs to apply a specific change, fix a bug, or simply reuse code. This article will expl
3 min read
How to Clone a Branch in Git?
Git is a popular version control system that allows developers to track changes in their code and collaborate with others. Cloning a branch in Git involves creating a copy of a specific branch from a remote repository. This article will guide you through the process of cloning a branch in Git. Table
3 min read
How to Delete All Local Branches in Git?
Git provides a powerful version control system that allows developers to collaborate efficiently on projects. Over time, however, a repository can have a lot of local branches that are no longer needed, cluttering the workspace and potentially causing confusion. The "main" is the by default branch c
3 min read