Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse Apply a commit to working copy

Tags:

git

revert

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.

like image 475
berkes Avatar asked Apr 17 '13 12:04

berkes


People also ask

How do I reverse a commit?

The easiest way to undo the last Git commit is to execute the “git reset” command with the “–soft” option that will preserve changes done to your files. You have to specify the commit to undo which is “HEAD~1” in this case. The last commit will be removed from your Git history.

Can we roll back the commit?

No, you can't undo, rollback or reverse a commit.

What happens when you reverse a commit?

A revert operation will take the specified commit, inverse the changes from that commit, and create a new "revert commit". The ref pointers are then updated to point at the new revert commit making it the tip of the branch.


3 Answers

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.

like image 102
Gareth Avatar answered Oct 19 '22 11:10

Gareth


Another way that works is git show | git apply -R. -R stands for reverse the diff.

This works with any diffs, so you can do git diff [...] | git apply -R

Or reverse a stash git stash show -p stash@{0} | git apply -R

like image 22
dosentmatter Avatar answered Oct 19 '22 11:10

dosentmatter


If you are after previous commit(s). Then the simplest way is

git reset HEAD~ -- .

what it does it leaves your working tree intact but changes index to match the previous commit. Git diff and GUI tools will highlight changes and allow you to go through them to remove, revert or change some of the hunks. Subsequent git -a commit --amend / git -a commit will fix either current commit or create a separate fix commit, while git reset --hard will abandon them.

To pick an arbitrary change and leave tree at HEAD.

git revert -n <changeset> # or changeset range
git reset -s HEAD . # restore worktree to the tip (but leave staging as above)
like image 1
user377178 Avatar answered Oct 19 '22 11:10

user377178