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? Commented Mar 24, 2015 at 15:02
  • Thanks ROMA, I guess so :-) Commented Mar 24, 2015 at 15:05
  • Sounds like you should use cherry-pick then ;) Commented Mar 24, 2015 at 15:06
  • possible duplicate of How do I "un-revert" a reverted Git commit? Commented Mar 24, 2015 at 15:12

2 Answers 2

4

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> 
Sign up to request clarification or add additional context in comments.

Comments

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 Comments

Except then you are possibly rewriting public history, which is bad.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.