How to check out a remote Git branch?

8    Asked by KatherineDwivedi in Devops , Asked on Apr 2, 2025

How can you switch to a branch that exists on a remote Git repository? What commands should you use to fetch and check out a remote branch locally? Let’s explore the steps to do it!

Answered by Helena Smith

If you want to work on a branch that exists on a remote Git repository, you need to fetch it first and then check it out locally. Here’s how you can do it step by step:

1. Fetch the Remote Branches

Before checking out a remote branch, you should update your local repository with the latest remote branches:

  git fetch origin

  • This updates your local Git repository with the latest remote changes.
  • It does not automatically check out the branch.

2. List All Remote Branches

To see available remote branches, run:

  git branch -r

This displays all branches available on the remote repository.

3. Check Out the Remote Branch Locally

Once you know the branch name, check it out using:

  git checkout -b my-branch origin/my-branch

  • -b creates a new local branch that tracks the remote one.
  • origin/my-branch is the remote branch name.

4. Alternative: Use git switch (Newer Versions of Git)

For Git 2.23+, you can use switch instead of checkout:

  git switch --track origin/my-branch

This is the recommended way in newer Git versions.

Final Tips:

  •  Make sure to commit your local changes before switching branches.
  •  If the branch already exists locally, you can simply run:

  git checkout my-branch

 Use git pull after switching to ensure your branch is up to date.



Your Answer

Interviews

Parent Categories