Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ScottJWalter/cd9714bd82f1b0e33591421bb6bd202e to your computer and use it in GitHub Desktop.

Select an option

Save ScottJWalter/cd9714bd82f1b0e33591421bb6bd202e to your computer and use it in GitHub Desktop.
How to Cherry pick commits across forks

Cherry picking commits across forks

When working with forks in Git, you may need to pull specific commits from one fork to another. This can be useful when you want to include a specific change from one fork to another without merging the entire branch.

From another fork to your fork

From your fork to another fork

From another fork to your fork

  1. Identify the commit hash(es) of the commit(s) you want to cherry-pick. You can find this hash on GitHub or by using git log after fetching.

  2. Add the "other" fork as a remote to your local repository.

git remote add <name-for-other-fork> <other-fork-remote-address>

<name-for-other-fork> is a symbolic name you will use to refer to the fork in your local repository. <other-fork-remote-address> is the clone URL or SSH address of the other fork.

  1. Fetch the commits from the other fork.
git fetch <name-for-other-fork>
  1. Check out the branch that you want to apply the commit to.
git checkout <target-branch-name>
  1. Make a new branch to apply the cherry-picked commit(s) that can be used to create a PR
git checkout -b <cherry-pick-pr-branch-name>
  1. Cherry-pick the commit(s) from the other fork.
git cherry-pick <commit-hash>

or several commits

git cherry-pick <commit-hash1> <commit-hash2> ...

or a commit range

git cherry-pick <commit-hash1>..<commit-hashN>
  1. Resolve any conflicts that may arise during the cherry-pick process. Git will prompt you to resolve conflicts if they occur. After resolving conflicts, you will need to add the changes and continue the cherry-pick process.
git add <conflict-file>
git cherry-pick --continue
  1. Push the new branch with the cherry-picked commit(s) to your fork.
git push origin <cherry-pick-pr-branch-name>
  1. Create a pull request from the new branch in your fork to the target branch in your fork.

From your fork to another fork

If you want to create a pr to apply a commit(s) from your fork to another fork, the steps are the same but we need to use the other fork as the target.

  1. Identify the commit hash(es) of the commit(s) you want to cherry-pick. You can find this hash on GitHub or by using git log after fetching.

  2. Add the "other" fork as a remote to your local repository.

git remote add <name-for-other-fork> <other-fork-remote-address>

<name-for-other-fork> is a symbolic name you will use to refer to the fork in your local repository. <other-fork-remote-address> is the clone URL or SSH address of the other fork.

  1. Fetch the commits from the other fork.
git fetch <name-for-other-fork>
  1. Create a new branch from the remote branch that you want to apply the commit to.
git checkout -b <cherry-pick-pr-branch-name> <name-for-other-fork>/<target-branch-name>
  1. Cherry-pick the commit(s) from your fork.
git cherry-pick <commit-hash>

or several commits

git cherry-pick <commit-hash1> <commit-hash2> ...

or a commit range

git cherry-pick <commit-hash1>..<commit-hashN>
  1. Resolve any conflicts that may arise during the cherry-pick process. Git will prompt you to resolve conflicts if they occur. After resolving conflicts, you will need to add the changes and continue the cherry-pick process.
git add <conflict-file>
git cherry-pick --continue
  1. Push the new branch with the cherry-picked commit(s) to the other fork.
git push <name-for-other-fork> <cherry-pick-pr-branch-name>

NOTE: it is very important that you include the other fork name and branch name in the push command to ensure that the branch is pushed to the correct fork. Suppress your instincts to use origin in this command.

  1. Create a pull request from the new branch in the other fork to the target branch in the other fork.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment