How to Cherry Pick a Commit from One Repository to Another in Git

Being a WordPress theme and plugin contributor, I often find myself in the position where I have to maintain multiple versions of a theme or plugin – free and premium. The so-called freemium model works well but it has a few downsides. One of them is that you need to maintain 2 codebases that share similar code. Sometimes you need to add new features or fix bugs and most often than not, you would need to make changes in two different repositories. You would typically develop the feature inside one of the two themes or plugins and then copy the code line by line, add it to the other theme or plugin and thoroughly test the code in both codebases to make sure it works. No need to say, if you have many themes and plugins, this can quickly become a nightmare. Luckily, there is a fancy tool that we as developers can take advantage of: cherry-picking commits from one git repository to another.

What is cherry-picking?

Unlike picking cherries, which often involves eating them, cherry picking in the context of git is the act of picking a commit from a branch and applying it to another branch. A popular use case for cherry picking could be a commit that has been pushed by mistake to the wrong branch. Another use case is to copy commit to another branch. There is no need to redo the same changes on the other branch, when you can just copy the changes to the other branch. Normally, you can do that with the merge command but there are specific cases when you cannot do that, i.e. different product versions, etc. In such cases, cherry picking is your best friend.

Please note that cherry picking is a controversial git command and some developers are strongly opposing it. It is a powerful but also dangerous tool. Use it only if you know what you are doing and if your team has integrated it into its workflow.

How to cherry pick a commit to another repository

Now, let me show you how to add a commit from one repository to another using the cherry pick git command.

Step 1: Add a Remote to the Source Repository: In your target repository (RepoB), the one where you want to cherry pick the commit you have done, you need to add a remote reference to the source repository (RepoA). This remote reference will allow you to fetch all the commits from the source repository.

git remote add {repo-name}{user}/{repo-name}

Replace {user} with the GitHub username and {repo-name} with the name of the repository where you have commited your changes.

Step 2: Fetch Commits from the Source Repository: After adding the remote reference, use the git fetch command to retrieve commits from the source repository.

git fetch {repo-name}

The fetch command command comes extremely handy here, since we do not want to merge the code (what git pull will do) but only to have the commits in place.

Step 3: Cherry-Pick the Desired Commit: Identify the commit you want to cherry-pick from the source repository (RepoA). Obtain its commit SHA hash and use the following command to apply it to the current branch in your target repository (RepoB). Go to RepoA and use the following command to see your list of commit and identify the SHA hash of the commit you want to cherry pick:

git log --oneline

Replace {commit-sha} with the actual commit SHA hash you want to cherry-pick.

git cherry-pick {commit-sha}

Step 4: Resolve Conflicts (if any): In some cases, there might be conflicts during the cherry-pick process. If conflicts occur, Git will pause the cherry-pick and inform you about the conflicting files. You need to manually resolve these conflicts by editing the affected files. After resolving conflicts, you can use the following commands to continue the cherry-pick process:

git add .  # Add resolved filesgit cherry-pick --continue  # Continue cherry-picking process

Step 5: Remove the Remote Reference: Once you’ve successfully cherry-picked the commit and resolved any conflicts, you can remove the remote reference to the source repository.

git remote remove {repo-name}

This concludes the process of cherry-picking a commit from one Git repository to another.

To Cherry Pick or not to Cherry Pick

Cherry-picking is a powerful tool, but it needs to be used with great caution. It’s best suited for applying small, isolated changes between repositories. For larger-scale changes or ongoing synchronization, you might consider other strategies like maintaining a shared library or using Git submodules. However, if it fits your workflow, just use it.