Data report"State of code review 2024" is now liveRead the full report

Git pull remote branches

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 explain how to pull a remote branch using Git, detailing the necessary commands and procedures to ensure you can effectively synchronize your local repository with changes made remotely.

Before diving into the specifics of pulling a remote branch, it's essential to understand some foundational concepts in Git:

  • Remote repository: A version of your project that is hosted on the internet or some other network, which you can push to and pull from.
  • Branch: A separate line of development in your project, which allows you to develop features, fix bugs, or experiment without affecting the main codebase. Can be thought of as a collection of commits.
  • Fetch: The command git fetch downloads branches and their respective commits from the remote repository but doesn't merge them into your current working files.
  • Pull: The command git pull is essentially a combination of git fetch followed by git merge. It fetches changes from the remote repository and then merges them into your local branch.

Before pulling changes from a remote branch, verify your remote connections with:

Terminal
git remote -v

This command lists all remote connections you have configured, showing URLs associated with each remote. This information is crucial to ensure you're interacting with the correct remote repository.

To see what branches are available from your remote repositories, use:

Terminal
git fetch --all

This command fetches all branches from all your remotes. To fetch branches from a specific remote, such as origin, you can use:

Terminal
git fetch origin

If you know the specific branch you want to fetch, you can use:

Terminal
git fetch origin <branch-name>

To pull changes from a remote branch and merge them into your local branch, follow these steps:

  1. Switch to your target branch: Ensure you are on the branch into which you want to pull changes. If not, switch to it using:

    Terminal
    git checkout <branch-name>
  2. Pull the remote branch: Use the following command to fetch and merge changes from the remote branch:

    Terminal
    git pull origin <branch-name>

    This command fetches the specified branch from the remote named origin and merges it into the branch you are currently working on.

When pulling a remote branch, you may encounter conflicts—differences that Git can't automatically resolve. You'll need to manually resolve these conflicts. Git will mark the files that have conflicts, and you will edit them to resolve the differences. After resolving conflicts, you'll add the resolved files to the staging area with:

Terminal
git add <file-name>

Then, continue the merging process by committing the changes:

Terminal
git commit -m "Resolve merge conflicts"

For a more detailed walkthrough, see this guide on resolving Git merge conflicts.

If you're working with a branch that doesn't exist on your local machine, you'll first need to fetch and check out the branch:

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

This set of commands creates a new local branch that tracks the remote branch.

For further reading on pulling remote branches in Git, see the official Git documentation.

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2