Switch to a Branch

GitGitBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In Git, branches are used to create different versions of a project. Switching between branches is a common task when working with Git. In this lab, you will learn how to switch to an existing branch in a Git repository.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/BranchManagementGroup(["`Branch Management`"]) git/BranchManagementGroup -.-> git/branch("`Handle Branches`") subgraph Lab Skills git/branch -.-> lab-12762{{"`Switch to a Branch`"}} end

Switch to a Branch

You have been working on a project in a Git repository named https://github.com/labex-labs/git-playground. Your team has created a new branch named feature-1 to work on a new feature. You need to switch to the feature-1 branch to continue working on the feature.

  1. Clone the Git repository:
git clone https://github.com/labex-labs/git-playground.git
  1. Navigate to the repository directory:
cd git-playground
  1. List all the branches in the repository:
git branch

Output:

feature-1
* master
  1. Switch to the feature-1 branch:
git checkout feature-1

Output:

Switched to branch 'feature-1'
  1. Verify that you are now on the feature-1 branch:
git branch

Output:

* feature-1
master

Summary

Switching to an existing branch in a Git repository is a common task when working with Git. Use the git checkout <branch> command to switch to the specified branch. In newer versions of Git, you can also use git switch <branch>.

Other Git Tutorials you may like