DEV Community

David Berri
David Berri

Posted on • Originally published at on

How to use git cherry-pick

This command is extremely useful when you need only one or few commits from one branch in another branch, but don't want or cannot merge.

Let's say that in your application you use the branch main to deploy to production and develop new code in another branch, say dev. You implement a few features, but realize one of them needs to be merged to main soon and cannot wait for you to finish whatever you were developing in the dev branch.

You can use the following git commands:

# Find and copy the commit hash that you want to send to another branch
git log

# change to the branch where this commit is needed
git checkout <branch-name>

# execute the cherry pick using the hash you copied earlier
git cherry-pick <commit-hash>
Enter fullscreen mode Exit fullscreen mode

If there are any conflicts, resolve them and commit the new code. Done.

Top comments (0)