Read Anthropic’s case study about Graphite Reviewer

How to cherry-pick commits from another repository 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.


Cherry-picking in Git allows you to select a specific commit from one repository and apply it to another. This can be useful when you want to incorporate specific changes without merging a whole branch of development. This guide will cover the steps required to cherry-pick a commit from another repository, whether it's a separate project, a fork, or a different remote branch within the same project.

When you execute git cherry-pick, you are essentially telling Git to take the changes made in a specific commit from one branch and apply them as a new commit on the branch you are currently checked out to. This does not move the commit itself; instead, it duplicates the commit content.

The basic syntax of the git cherry-pick command is:

Terminal
git cherry-pick <commit-hash>

Where <commit-hash> is the hash ID of the commit you want to cherry-pick. This hash represents a unique identifier for each commit.

Join 20,000+ developers at top companies
Stop wrestling with Git commands
The Graphite CLI takes all the pain out of Git, allowing you to ship faster and stop googling Git commands.
main
diff1
diff2
  1. Identify the commit to cherry-pick: First, you need to find the commit hash of the commit you wish to cherry-pick. This can usually be found via git log.

  2. Cherry-pick the commit: Run the git cherry-pick command with the commit hash:

    Terminal
    git cherry-pick 1a2b3c4d

    Replace 1a2b3c4d with the actual commit hash.

  3. Resolve conflicts: If the changes in the cherry-picked commit conflict with other changes in your branch, Git will pause the process and ask you to resolve the conflicts. After resolving conflicts, you would typically add the resolved files with git add, and then continue the cherry-pick process by running:

    Terminal
    git cherry-pick --continue
  4. Abort the process: If you decide not to continue with the cherry-pick due to conflicts or other reasons, you can abort the process:

    Terminal
    git cherry-pick --abort

You can also cherry pick commits from a completely different repository.

If you haven't already cloned the repository containing the commit you want to cherry-pick, you'll need to do so:

Terminal
git clone <repository-url>

Replace <repository-url> with the URL of the Git repository from which you want to cherry-pick the commit.

This adds a copy of the selected repository to your local machine.

If you already have a local repository and just need to access a commit from another repository, you can add that repository as a remote:

Terminal
git remote add source-repo <repository-url>

Replace source-repo with a name that identifies the remote repository, and <repository-url> with the URL of the repository.

This will allow you to access the repository remotely.

Whether you've cloned a new repository or added a remote to an existing one, the next step is to fetch the changes:

Terminal
git fetch source-repo

This command fetches all branches and their respective commits from the newly added remote repository (named source-repo). Now you have access to all the commits from the source repository in your local environment.

Join 20,000+ developers at top companies
The best engineers use Graphite to simplify Git
Engineers at Vercel, Snowflake & The Browser Company are shipping faster and staying unblocked with Graphite.
main
diff1
diff2

Before you can cherry-pick a commit, you need to identify it. You can list the recent commits:

Terminal
git log source-repo/main --oneline

Replace source-repo/main with the appropriate remote branch that contains the commit. This command will display commits from the source repository's main branch, allowing you to find the commit hash (a short SHA-1 hash) of the commit you want to cherry-pick.

Once you have the commit hash, you can apply that commit to your current branch:

Terminal
git cherry-pick <commit-hash>

Replace <commit-hash> with the actual hash of the commit you want to cherry-pick.

Cherry-picking can lead to conflicts if the changes in the commit are incompatible with the current branch's state. If there are conflicts, Git will stop the cherry-pick process, and you'll need to resolve these conflicts manually:

  • Open the conflicted files and make the necessary changes.
  • After resolving conflicts, stage the changes using git add.
  • Once all conflicts are resolved and the changes are staged, continue the cherry-pick process by running:
Terminal
git cherry-pick --continue

After successfully cherry-picking the commit, you might want to push the changes to your remote repository:

Terminal
git push origin <branch-name>

Replace <branch-name> with the name of the branch to which you've cherry-picked the commit.

For further reading on the git cherry-pick command, see the official Git documentation.

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