Reverse Apply a commit to working copy

Spread the love

Question Description

In order to investigate the effect introduced by a previous commit, I want to reverse- apply it to my working copy and fiddle around with the code.

I managed with a workflow around creating and applying a patch, but wonder if this can be done easier.

git checkout -b "tmp-fiddle"
git diff -R -p d9fd2bb^ d9fd2bb > patch_to_examine.patch
# Manually edit the patch a little
git apply patch_to_examine.patch

Note that I am not looking at git revert or git rebase -i since these would either introduce a new commit or change the history: I merely want the changes introduced in d9fd2bb to be un-applied to my current working copy.

Practice As Follows

How about git revert -n?

-n
--no-commit

Usually the command automatically creates some commits with commit log messages stating which commits were reverted. This flag applies the changes necessary to revert the named commits to your working tree and the index, but does not make the commits. In addition, when this option is used, your index does not have to match the HEAD commit. The revert is done against the beginning state of your index.

This is useful when reverting more than one commits’ effect to your index in a row.

Leave a Comment