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

How to pull from another branch in Git

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

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


Pulling changes from another branch in Git helps synchronize your work with updates made elsewhere in the repository. This guide describes how to pull in changes from other branches, including how to pull only specific commits or files.

git pull is a command in Git that fetches changes from a remote repository and then merges them into your current branch. It effectively combines the actions of git fetch followed by git merge.

Here’s a step-by-step guide on how to pull changes from another branch within your local repository or from a remote repository.

If you need to integrate changes from another branch within your local repository, you can follow these steps:

  1. Switch to your target branch:

    First, make sure you're on the branch that needs to receive the changes.

    Terminal
    git checkout my-branch
  2. Merge the changes from the other branch:

    Then, merge the changes from the branch you want to pull from. Replace other-branch with the name of the branch you're pulling from.

    Terminal
    git merge other-branch

    This command will merge the latest committed changes from other-branch into my-branch.

If you need to pull specific commits rather than all changes from another branch, you can use the git cherry-pick command:

  1. Find the commit IDs you want to pull:

    Use git log to find the commit IDs in the other branch.

    Terminal
    git checkout other-branch
    git log
  2. Cherry-pick the commits:

    Switch back to your branch and use git cherry-pick followed by the commit IDs.

    Terminal
    git checkout my-branch
    git cherry-pick <commit-ID>

To pull changes from a remote branch, you can specify the remote and branch explicitly:

  1. Fetch the specific branch:

    Terminal
    git fetch origin other-branch
  2. Merge the fetched branch:

    Terminal
    git merge origin/other-branch

    Alternatively, you can use git pull directly if you're on the branch that you want to update:

    Terminal
    git pull origin other-branch

To pull specific files from another branch:

Terminal
git checkout other-branch -- path/to/file

This command will bring the specified file from other-branch into your current branch.

  • Always make sure you're on the correct branch before pulling or merging changes.
  • Regularly commit your changes before pulling from another branch to avoid conflicts.
  • Use git status to check your working directory and staging area before and after pulling changes.
  • Resolve conflicts carefully when they occur during a merge.

For further reading, see this guide on using the git pull command.

Git gud
"It's the first Git workflow I've used that actually feels good."
–@robboclancy
Learn more

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