Introduction
🧑💻 New to Git or LabEx? We recommend starting with the Quick Start with Git course.
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.
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.
- Clone the Git repository:
git clone https://github.com/labex-labs/git-playground.git
- Navigate to the repository directory:
cd git-playground
- List all the branches in the repository:
git branch
Output:
feature-1
* master
- Switch to the
feature-1branch:
git checkout feature-1
Output:
Switched to branch 'feature-1'
- Verify that you are now on the
feature-1branch:
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>.