Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cherry pick commits after they've been reverted?

I was working on my feature branch and after review, merged it into development to be deployed. Later, a coworker decided to do a release and merged his and mine into master. While deploying he realized his code was buggy and reverted master.

In our fork-and-pull flow, that means that now development and master are both reverted.

When I came in this morning, I rebased from development per usual, to learn afterward there had been a revert.

Now I'm trying to cherry-pick my work from the original feature branch only to realize it gives me "empty commit messages" because of the revert.

  1. is this because revert is a mirror image of my previous commits?
  2. is there a way to revert the revert? (makes my head hurt)
  3. is there anyway to get my commits to show up in the diff now that my I have rebased

Any help is greatly appreciated.

like image 750
Gina Avatar asked May 15 '14 14:05

Gina


People also ask

Can I cherry pick reverted commit?

The git revert commit command is substantially similar to the command git cherry-pick commit with one important difference: it applies the inverse of the given commit . Thus, this command is used to introduce a new commit that reverses the effects of a given commit.

How do you restore cherry picks?

A cherry-pick is basically a commit, so if you want to undo it, you just undo the commit. Stash your current changes so you can reapply them after resetting the commit.

How do you undo a failed cherry pick?

I found the answer is git reset --merge - it clears the conflicted cherry-pick attempt.

What happens if you cherry pick the same commit twice?

If a commit being cherry picked duplicates a commit already in the current history, it will become empty.


1 Answers

Cherry-pick and rebase checks the patch id of the commit (basically a hash of the change) and already sees that the change exists on the branch, so it doesn't pick it. Rebasing can sometimes work because changes in the files may have caused the actual diff to change a bit--resulting in a different patch id--but that doesn't appear to be the case for you.

You can "revert the revert", though that will re-introduce the broken bits your co-worker introduced. Then you need to revert the buggy commits your co-worker made. It's a lot of reverting, and quite a bit to keep straight, so take it slow and have someone sit with you just to make sure you don't miss anything.

Another choice might be to replay your commits by doing git show COMMIT_ID | git apply. This re-applies the diff to your working tree. As long as your tree is clean, you can use git commit -C COMMIT_ID to re-use the message from the original commit. You can probably use git format-patch and git am to avoid the extra steps.

like image 54
John Szakmeister Avatar answered Nov 03 '22 19:11

John Szakmeister