How to Check Out a Remote Branch in Git

A teammate asks you to review feature-login, but the branch does not appear in your local branch list.

If you already have the repository, you usually do not need to clone it again. Fetch the remote updates, then create a local branch that tracks the remote branch.

1. Check your working changes

Open a terminal inside the repository and run:

git status

Commit or stash unfinished work if you need it kept separate before switching branches. Avoid forcing a switch over changes you want to preserve.

2. Fetch the remote updates

git fetch origin

This downloads objects and updates remote-tracking references according to the remote’s fetch configuration. It does not merge those changes into your current branch.

The examples use a remote named origin. Check your remote names with:

git remote -v

3. Find the branch

git branch -r

Look for an entry such as:

origin/main
origin/feature-login

origin/feature-login is a local reference representing the remote branch as of your last fetch. It is not a continuously live view of the server.

4. Create a local tracking branch

git switch -c feature-login --track origin/feature-login

This creates the local branch, configures its upstream and switches to it.

If you prefer a different local name:

git switch -c review-login --track origin/feature-login

The local branch name does not have to match the remote branch name.

5. Confirm the setup

git status
git branch -vv

Check that the current branch tracks origin/feature-login. Any ahead or behind information is based on your locally available references, so fetch again when you need an updated comparison.

What if the local branch already exists?

Switch to it without creating another branch:

git switch feature-login

If its upstream is missing, configure it separately:

git branch --set-upstream-to=origin/feature-login feature-login

Switching to an existing branch does not automatically update its commits from the remote.

What if you do not have the repository?

Clone it first. To start with a particular branch:

git clone --branch feature-login --single-branch REPOSITORY_URL

Replace REPOSITORY_URL with the actual repository URL. The --single-branch option limits the initial clone and default future fetch configuration to that branch.

Branch still missing?

  • Check the spelling and remote name.
  • Confirm that your teammate has pushed the branch.
  • Check your repository access.
  • If you used a single-branch clone, inspect the fetch configuration because other branches may be excluded.

The distinction is useful: clone creates a local repository, fetch updates remote information, and switch selects the local branch you will work on.

Further reading:

No comments