Open In App

How to Switch Branch in Git: A Complete Guide for Beginners

Last Updated : 05 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Screenshot-2024-04-18-185607

Step 2: Switch to the main branch

git checkout main

Screenshot-2024-04-18-185703

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

Screenshot-2024-04-18-185857

Step 2: Switch to the feature-new branch

git switch feature-new

Screenshot-2024-04-18-185930

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>

Detached HEAD in Git

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.


Next Article
Article Tags :

Similar Reads