Git

How to Undo a Successful “git cherry-pick”?

Git has the functionality to revert the changes according to the requirements of the developers through multiple commands, such as the “git cherry-pick” command. It can be used when developers need to revert the previously added changes with the help of desired Git commit SHA-hash reference and append to the current position of the HEAD pointer. Additionally, Git users can undo the performed “git cherry-pick” operation.

This blog will discuss:

How to Use “git cherry-pick” Command?

The “git cherry-pick” command is the most useful when it is required to undo the existing modifications by selecting the Git repository commit through reference and appended to the current working HEAD position.

To do so, navigate to the Git repository and view the content list. Then, modify the required files and push changes to the repository. Next, remove all files with the provided extension, commit changes, and select the desired commit. Lastly, execute the “git cherry-pick <commit-id>” command.

Step 1: Go to Git Repository

Switch to the required git repository using the “cd” command:

$ cd "C:\Users\nazma\Git\test1"

Step 2: View Content List

Run the “ls” command and display the list of existing content:

$ ls

Here, the highlighted files are selected for further processing:

Step 3: Update Selected File

Now, execute the “echo” command to modify the selected file:

$ echo "first text file" >> file1.txt

Step 4: Push Changes to Index

Next, push the changes to the staging area through the “git add” command:

$ git add file1.txt

Step 5: Update Git Repository

After that, utilize the below-listed command and save the added changes to the Git repository:

$ git commit -m "file1.txt updated"

Step 6: Modify Another Selected File

Run the “echo” command and modify the previously selected another file:

$ echo "second text file" >> file2.txt

Step 7: Update Staging Index

Now, update the staging area through the below-stated command:

$ git add file2.txt

Step 8: Save Changes to Git Repository

Utilized the “git commit” command to save the added changes to the local repository:

$ git commit -m "file2.txt updated"

Step 9: Remove All Files having With “.txt” Extension

Now, delete files from the Git repository which have the “.txt” extension by running the “rm” command:

$ rm *.txt

Here, the asterisk “*” symbol is used to fetch all files having the provided extension:

Step 10: Track Changes

Next, use the “git add .” command to track all added changes from the working area to the staging index:

$ git add .

Step 11: Update Git Repository Status

After that, push all staged changes to the Git repository with the help of the “git commit” command:

$ git commit -m "text file deleted"

Step 12: View Git Repository Reflog History

To display the Git repository reflog history, use the “git reflog .” command:

$ git reflog .

In the below-given output, the highlighted commit is our target HEAD pointer, so we will copy its SHA-hash Id:

Step 13: Cherry Pick Commit

Execute the “git cherry-pick” command along with the selected commit reference Id and change the HEAD pointer to it:

$ git cherry-pick 1193660

Step 14: Verify Cherry Pick Operation

Now, check the Git log history through the “git reflog .” command:

$ git reflog .

As you can see, the current position of the HEAD pointer changed to provided commit, and the rest of the changes are reverted:

Check out the next section to undo the successful “git cherry-pick” operation.

How to Revert a Successful “git cherry-pick”?

Try the following steps to revert the performed “git cherry-pick” operation.

First, execute the “git reset” command along with the “–hard” option and desired index pointing, such as “HEAD^”:

$ git reset --hard HEAD^

According to the below-given output, the HEAD move to the previous commit Id:

To ensure that the HEAD is reverting to the previous position, use the “git reflog .” command:

$ git reflog .

It can be observed that the “git cherry-pick” operation is undone successfully:

That’s all! We have provided the easiest way of undoing the successful “git cherry-pick” operation.

Conclusion

The “git cherry-pick” command is used when users want to undo the existing changes by selecting the commit through reference. To do so, go to the local Git directory and list its content. Then, modify the required files and push changes to the repository. Next, remove all files with the provided extension, commit changes, and select the desired commit. Execute the “git cherry-pick <commit-i’d>” command. To undo this operation, execute the “git reset –hard HEAD^” command. This blog illustrated the way of undoing the successful “git cherry-pick” operation.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.