Lisa Anne
Lisa Anne

Reputation: 4595

Git: how to revert a revert

This is my current situation.

I have erroneously merged a pull request that contained rubbish.

So I reverted it using git revert -m 1 <sha of commit>

Now I wish to undo that revert but this time cherry pick only the correct changes.

Please how do I do that?

Thanks

Upvotes: 4

Views: 4077

Answers (2)

Vitalliuss
Vitalliuss

Reputation: 1732

Revert in git is a commit too. So you can simply revert a revert as usual commit:

git revert <revert_you_want_to_undo_sha1>

Upvotes: 4

kjlubick
kjlubick

Reputation: 1318

You can use git reset to remove the bad commits (including the revert).

Assuming your history looks like:

good -> good -> good -> rubbish -> revertRubbish

You can simply do git reset HEAD~2 to make your version history look like:

good -> good -> good 

If it's more complicated than that, say

good1 -> good2  -> rubbish -> good3 -> good4 -> revertGood4 -> revertGood3 -> revertRubbish

You may have to do something like:

git reset HEAD~6  //Deletes last 6 commits
git cherry-pick good3
git cherry-pick good4

with a resulting history of

good1 -> good2  -> good3* -> good4*

The asterisks indicate the code changes will be the same, just with a different hash than the original commits.

Upvotes: 2

Related Questions