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

4
  • So you don't want to revert the revert, you just want to cherry-pick a few commits into your branch?
    – Roman
    Mar 24, 2015 at 15:02
  • Thanks ROMA, I guess so :-)
    – Lisa Anne
    Mar 24, 2015 at 15:05
  • Sounds like you should use cherry-pick then ;)
    – Roman
    Mar 24, 2015 at 15:06
  • possible duplicate of How do I "un-revert" a reverted Git commit?
    – jub0bs
    Mar 24, 2015 at 15:12

2 Answers 2

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>
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.

2
  • Except then you are possibly rewriting public history, which is bad.
    – Roman
    Mar 24, 2015 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, 2015 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.