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
-
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 logafter fetching. -
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.
- Fetch the commits from the other fork.
git fetch <name-for-other-fork>- Check out the branch that you want to apply the commit to.
git checkout <target-branch-name>- 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>- 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>- 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- Push the new branch with the cherry-picked commit(s) to your fork.
git push origin <cherry-pick-pr-branch-name>- Create a pull request from the new branch in your fork to the target branch in your 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.
-
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 logafter fetching. -
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.
- Fetch the commits from the other fork.
git fetch <name-for-other-fork>- 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>- 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>- 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- 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.
- Create a pull request from the new branch in the other fork to the target branch in the other fork.