The git cherry-pick command is an extraction operation that allows you to select an arbitrary commit from any branch and apply exactly those changes as a new commit on your current branch.
Imagine this situation: You are working on an experimental branch (feature/nueva-interfaz) and you have 20 commits of unstable changes.
While working on your branch, you realize you have fixed a critical bug that also affected the main main branch.
- You cannot Merge: If you merge
featureintomain, you also bring all the unstable garbage, it’s not ready. - But it’s also not a matter of replicating all the changes manually in
main(with the risk of missing something).
You need a way to take only that specific commit and apply it to main, ignoring the rest. For this precision operation, git cherry-pick exists.
What is Cherry-pick?
As the name suggests, this command allows you to select an arbitrary commit from anywhere in the repository and apply it to your current branch.
cherry-pick does not move the commit. What it does is read the changes introduced by that commit and create a new copy (with a new Hash) on your current branch.
How to do a Cherry Pick
Let’s look at the most common use case. You have a bug in main, and you have fixed it by chance in the develop branch in commit a1b2c3d.
You want to bring that fix to main now. The steps would be as follows:
Identify the target
First you need to know the Hash (SHA-1) of the commit you want to copy.
git log --oneline develop
Let’s assume the commit is: a1b2c3d Corregir error de cálculo en el carrito
Position yourself at the destination
We move to the branch where we want to paste the commit.
git switch main
Execute the Cherry-pick
We run the command:
git cherry-pick a1b2c3d
Git will take the changes introduced in a1b2c3d and create a new commit on main with the same message and the same author, but with the content applied on top of main.
Done! Bug fixed in production without dirtying the branch with experimental code.
Conflicts during Cherry-pick
Just like in a merge or a rebase, when trying to apply a patch from one branch to another, conflicts can arise if the code base is very different.
If this happens, Git will pause the process.
- Resolve the conflicts in your editor (VS Code).
- Add the files to the Stage:
git add . - Continue the operation:
git cherry-pick --continue
Or, if you change your mind, you can cancel everything with:
git cherry-pick --abort
The traditional disclaimer
cherry-pick is very powerful, but it should not be part of your daily workflow. Use it as an exception, not as the rule.
The problem is that you are duplicating changes. By doing cherry-pick you create a new commit with a different Hash.
If you cherry-pick commit X from develop to main, you now have two commits that are functionally identical but different for Git (X and X').
When in the future you do a merge from develop to main, Git will have to merge X with X'. Usually Git is smart and realizes they are the same (and ignores it), but sometimes it can generate conflicts.
If you find yourself doing cherry-pick constantly, it’s a sign that you are working on the wrong branch. Perhaps you should have made the fix in a separate hotfix branch and then merged it into both main and develop.
