Select Page

Introduction

The cherry-pick command in git allows you to move a group of commits from a given branch to another branch. This command gives rise to a different approach to git merge, and at times is used as an alternative for it. For instance, instead of using the git merge command to migrate commits from a different branch to the master branch, you can use the git cherry-pick command instead.

Why is it preferable to git merge, you may ask? As it appears to be, git merge clutters your master branch’s history by pushing in all the commits made in the separate branch. On the other hand, if you first collapse all the commits made in that separate branch into one single commit, you can use the git cherry-pick command afterwards to move that commit to your master branch.

However, it is possible that you realise you have used the cherry-pick command, but suddenly want to undo it. How will you revert the commit made by git cherry-pick command now?

This article will give you all the possible solutions to exactly just that – undoing a git cherry-pick, based on your situation, whether or not you have other local changes that you’d like to keep.


Using git reflog to locate commit

The important thing to realize here is that the git cherry-pick command creates a commit which works like a regular one, so it can be undone the same way a regular commit can be. All you need to ensure is that you have switched to the target branch where the cherry-pick command created the commit on. 

One of the common ways to undo a commit is by first looking at the output of the git reflog command, which will inform you of the HASH for the previous commit you just made. Using this HASH, we will be able to revert back to a previous commit and undo the git cherry-pick. 

There’s an even shorter way to do this, which is by skipping the reflog command and directly referencing the last HEAD, but it is probably not recommended as it may not work with older versions of git. Which is why, just to be safe, we will use the HASH from the output of git reflog

At first, use the following command in the repository where the commit was made:

$ git reflog

You should get an option that looks something like:

700a240 (HEAD -> master) HEAD@{0}: commit (initial): The second file
eb443a1 HEAD@{1}: commit (initial): The first file

Now don’t get too worried about needing to understand everything in the above output. The only thing you need here is the HASH, which is the first 7-digits you see in each line of the output. So the question is, which of the 2 HASH do you actually need?

Let’s say that there are more commits you have previously made, in which case, the output should have more lines The git reflog command will list all the commits you have created till now.

For now, make note of the HASH in the second line, which should have HEAD@{1}. The second recent commit in your repository should always have HEAD@{1}. In my case, the HASH is “eb443a1”. It can be anything else for your specific commit – doesn’t matter, simply make note of it somewhere.


Using git reset to undo cherry-pick

Using the HASH we found in the previous section by using the reflog command, we will now revert back to the previous commit and undo the git cherry-pick. To do this, you’d like to use the following command:

$ git reset <HASH> --hard

Replace the <HASH> with the actual HASH you noted. In my specific situation, the command should look something like:

$ git reset eb443a1 --hard

But before you proceed, you should hear me out first! If you use the above command, it will remove any other local changes you might have made, which means, if you have staged some files to be committed, but haven’t done that yet, they will also be removed with this command. If this is not something you need to worry about, you may simply use the above command to revert back to the previous commit. Otherwise, you’d want to follow the method mentioned in the next section. 


Using git stash to undo cherry-pick

If you want to keep the other changes, the only difference from the previous method is to include a git stash. You’ll still need to use the same reset command to revert back, but if you wish to recover the other changes, you first need to stash your repository by executing the following command:

$ git stash

After that, simply use the reset command we discussed earlier:

$ git reset <HASH> --hard

Once you’ve reverted back to the previous commit, you’d now like to executeL

$ git stash pop

Congratulations! You have successfully undone the git cherry-pick command with your local changes unremoved!