check-out
remote-branch
git

How do I check out a remote Git branch?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Checking out a remote branch is simple once you know the difference between a remote ref and a local tracking branch. Many Git mistakes happen because developers jump directly to checkout commands without confirming what exists locally and remotely. This guide shows a safe flow you can reuse in day to day work.

Inspect Remote State First

Start by synchronizing refs from your remotes. If your local remote refs are stale, you may try to check out a branch that was renamed or deleted.

bash
git fetch origin --prune
git branch -r

The first command updates remote tracking refs and removes refs that no longer exist on origin. The second command lists branches from the remote namespace so you can confirm the exact branch name.

If you are unsure whether a branch exists on the remote, check it directly:

bash
git ls-remote --heads origin feature/login-flow

If you get no output, the branch name is wrong or the branch is not on that remote.

Create a Local Tracking Branch

When a remote branch exists and you do not already have a local branch with the same name, create a tracking branch.

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

This creates a local branch named feature/login-flow and sets its upstream to origin/feature/login-flow. That upstream setting matters because git pull and git push can work without extra branch arguments.

You can verify tracking with:

bash
git branch -vv

Look for your branch and confirm it shows [origin/feature/login-flow].

If your Git version is older and does not support git switch, use:

bash
git checkout -b feature/login-flow origin/feature/login-flow

That command does the same core operation.

Work with Existing Local Branches Safely

If you already have a local branch, do not recreate it. Switch to it and ensure upstream is set correctly.

bash
git switch feature/login-flow
git branch --set-upstream-to=origin/feature/login-flow

This avoids accidental branch duplication such as feature/login-flow-2.

You may also see people run this:

bash
git switch --detach origin/feature/login-flow

Detached mode is useful for read only inspection, but commits made in detached mode are easy to lose if you do not create a branch afterward.

If you intentionally inspect in detached mode and decide to keep your work, recover safely:

bash
git switch -c hotfix/from-detached

Now your commits are anchored to a branch.

Automate the Flow in a Small Script

For teams that frequently jump to remote branches, a helper script reduces typing and avoids inconsistent commands.

bash
1#!/usr/bin/env bash
2set -euo pipefail
3
4remote="origin"
5branch="$1"
6
7git fetch "$remote" --prune
8if git show-ref --verify --quiet "refs/remotes/$remote/$branch"; then
9  if git show-ref --verify --quiet "refs/heads/$branch"; then
10    git switch "$branch"
11    git branch --set-upstream-to="$remote/$branch"
12  else
13    git switch -c "$branch" --track "$remote/$branch"
14  fi
15else
16  echo "Remote branch not found: $remote/$branch" >&2
17  exit 1
18fi

Save it as git-checkout-remote.sh, run chmod +x git-checkout-remote.sh, then call it with a branch name.

Common Pitfalls

The most common issue is skipping git fetch and using stale remote refs. Fix that by always fetching with --prune before checking out remote work.

Another frequent mistake is creating a local branch without an upstream and later seeing push errors. Run git branch -vv to confirm tracking and set upstream with git branch --set-upstream-to if needed.

A third issue is working in detached HEAD unintentionally. If git status says HEAD detached, create or switch to a branch before continuing development.

Summary

  • Fetch remote refs before checkout so you are using current branch metadata.
  • Prefer git switch -c ... --track ... to create a local tracking branch cleanly.
  • Use git branch -vv to verify upstream configuration.
  • Avoid detached HEAD for feature work unless you plan to inspect only.
  • Script the flow for repeatability and fewer command mistakes.

Course illustration
Course illustration

All Rights Reserved.