4

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

Lisa Anne
  • 4,482
  • 17
  • 83
  • 157

2 Answers2

3

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>
Vitalliuss
  • 1,614
  • 1
  • 12
  • 10
2

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.

KevinL
  • 1,238
  • 11
  • 28
  • Except then you are possibly rewriting public history, which is bad. – Roman Mar 24 '15 at 17:34
  • Yes, that is good to note that `git reset` should not be used on public history. If the OP has caught the problem before it made it into public space, or it is a repo with few contributors, then `git reset` is fine, in my opinion. Otherwise, OP will just have to be stuck with the messy pull request/revert/cherry-pick in the public history. – KevinL Mar 25 '15 at 14:11