Pick Commits from One Branch to Another Using git cherry-pick

git cherry-pick cover image - ai generated

Understanding the git cherry-pick command through a practical use case from my experience.

Use Case

You have been working on a branch abc and have pushed numerous commits to it. Now, you are working in a new branch xyz, which is a subset of abc, meaning there are a few files present in abc that you don’t want in xyz. You need the exact same code changes in xyz that you made in abc. Remember that both branches are different and you don’t intend to make it exactly same, so merging branch abc in xyz is out of question.

Instead of manually copying the changes, you can save time by using the git cherry-pick command to apply those commits from abc to xyz.

Let’s see how to do this.

Using git cherry-pick

Syntax

git cherry-pick <commit_hash>

This command will cherry-pick the specified commit and apply it to the current branch. (Note: Make sure you are on the branch where you want to apply the commit.)

Tips and Tricks

Suppose you need to cherry-pick a range of commits:

Screenshot showing a list of git commits

You don’t have to cherry-pick each commit individually. You can cherry-pick all the commits in one go using the following command:

git cherry-pick 0db04eb..5d2a08f

Note that the commit 0db04eb is not included in this range. To include it, use:

git cherry-pick 0db04eb^..5d2a08f

Additional Tips and Tricks

There are many other tips and tricks for using git cherry-pick that can make your workflow more efficient. For example, you can track where a particular commit was cherry-picked from or add a custom message during the cherry-pick for future reference. For more detailed information, I recommend reading this article on cherry-pick.