How to checkout remote branches in Git

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


This guide will help you understand how to efficiently work with remote branches using Git, covering everything from checking out a remote branch for the first time to dealing with branches from different remotes.

  • Remote branch: A branch hosted on a remote repository (like GitHub). You interact with it to share your work and collaborate with others.

  • Local branch: A branch in your local repository where you make and commit changes, stored directly on your machine.

First, ensure you have Git installed on your machine. Open your terminal (Linux/Mac) or Git Bash (Windows) to execute Git commands.

If you haven't already cloned the remote repository to your local machine, do so by running:

Terminal
git clone <repository-url>

For more info on git clone see: the difference between cloning a git repo with SSH and HTTPS.4. Fetching remote branches

Before you checkout a remote branch, make sure your local Git is aware of any existing remote branches:

Terminal
git fetch origin

This command fetches updated data from the remote named origin without merging changes into your local branches.

  • First Time Checkout:

    If you're checking out a remote branch for the first time, use:

    git checkout -b <branch-name> origin/<branch-name>

    This command creates a new local branch that tracks the remote branch.

  • Checkout and track a remote branch:

    To checkout an existing remote branch and set your local branch to track it:

    git checkout --track origin/<branch-name>

    Git automatically names your local branch the same as the remote one.

  • Checking out with a new local branch name:

    If you want to give the local branch a different name:

    git checkout -b <new-local-branch-name> origin/<branch-name>

You can fetch and checkout a remote branch in one step using:

Terminal
git fetch origin <branch-name>:<local-branch-name>
git checkout <local-branch-name>

If you're working with multiple remotes, you may need to checkout branches from different sources. When checking out a branch on a different remote, you can specify the remote name with:

Terminal
git checkout -b <local-branch-name> <remote-name>/<branch-name>

After checking out a remote branch, in order to update your local branch with the latest upstream changes, run:

Terminal
git pull

This command fetches changes from the remote and merges them into your local branch.

To create a new branch based on the current state of a remote branch, run:

Terminal
git checkout -b <new-branch-name> origin/<existing-branch-name>
  • Set upstream on push: When pushing a new local branch for the first time, run:

    git push -u origin <branch-name>

    This sets the remote branch as upstream for your local branch.

  • Checkout and set upstream together:

    For an existing branch, you can set the upstream during checkout:

    git checkout --set-upstream-to=origin/<branch-name> <local-branch-name>

  • Remote branch not found: Make sure you've run git fetch to update your list of remote branches.

  • Branch name conflicts: If there's a naming conflict ie. a branch already exists with that same name in the same repo, select a different name for your local branch. See: renaming git branches for more info.

  • Permissions: Ensure you have the necessary permissions to access the remote repository and its branches.

For further reading see the official git documentation on remote branches.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2