Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Cherry-picking commits

Header Image

Ahoy there mateys! In this article, we’ll be talking about one of the most useful Git commands out there: cherry-picking commits. This command allows you to selectively apply a commit from one branch to another, without having to merge the entire branch.

Selectively applying a commit to another branch

Let’s say you’re working on a feature branch and you realize that there’s a specific commit on another branch that you need to apply to your current branch. You could merge the entire branch, but what if there are other changes in that branch that you don’t want to include? This is where cherry-picking comes in.

To cherry-pick a commit, first switch to the branch that you want to apply the commit to using the git checkout command. Then, use the git cherry-pick command followed by the commit hash of the commit you want to apply. For example:

$ git checkout feature-branch
$ git cherry-pick abc123

This will apply the commit with the hash abc123 to your current branch. You can cherry-pick multiple commits by providing multiple commit hashes.

It’s important to note that cherry-picking a commit creates a new commit with a different hash. This is because the commit includes a different set of changes than the original commit on the other branch.

Conclusion

And there you have it, mateys! Cherry-picking commits is a handy tool in your Git arsenal. It allows you to selectively apply changes from one branch to another without having to merge the entire branch.

Keep in mind that cherry-picking a commit creates a new commit with a different hash, so be sure to double-check your commit history and make sure everything looks good before pushing to a remote repository.

We hope you found this article helpful. If you have any questions or comments, feel free to drop us a line in the comments below. Fair winds and following seas!