2

The situation: Bob merged his development branch BranchA into master (commit hash [c1]), then performed another commit on master (commit hash [c2]) to tweak something and remove some debug lines he forgot about. Then a few more commits are performed on master afterward (hashes [c3] and [c4]), that don’t touch any of the same code that [c1] and [c2] did.

Well, Bob didn't actually have permission to make those changes, and we now want to back them (both) out of master.

If we do git revert -m 1 [c1], it gives this error:

error: could not revert [c1]... Merged in BranchA (pull request #398)
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

So, how should we go about correctly removing that code? As a note, we would still like to keep commits [c3] and [c4] in our codebase -- we just need the merge that happened [c1] and the commit at [c2] taken back out.

Mike Todd
  • 7,227
  • 2
  • 15
  • 11
  • And just as a note, we would still like to keep commits [c3] and [c4] in our codebase -- we just need the merge that happened [c1] and the commit at [c2] taken back out. – Mike Todd Aug 13 '13 at 19:26
  • Please, when you have more info to add to a question, edit it rather than commenting on it. – Geeky Guy Aug 13 '13 at 19:29
  • possible duplicate of [How to delete a "git commit" ?](http://stackoverflow.com/questions/1338728/how-to-delete-a-git-commit) – Geeky Guy Aug 13 '13 at 19:29

1 Answers1

0

Rebase your Head~4 (because there were 4 commits - just use as many numbers as you want to go back). Then remove c1 and c2 while rebasing.

Some documentation on that: http://git-scm.com/book/en/Git-Branching-Rebasing

Just now, I also found this relevant question here in SO: Delete commits from a branch in Git

Community
  • 1
  • 1
Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
  • Looking at that other SO article, it seems like the best option may be to just create another branch that starts at the last "valid" state, and replay [c3] and [c4] onto it. Would that be the easiest here? We are moving from SVN to Git, so we're still learning all of the commands, and having some bad growing pains with reverts. EDIT: Or is this exactly what you suggested? :) – Mike Todd Aug 13 '13 at 19:52
  • I don't think it would be the easiest way. But it sure is the way which is less likely to mess your codebase really bad, because you're messing with a branch, not the trunk. – Geeky Guy Aug 13 '13 at 19:57
  • I would personally go for a `git rebase HEAD~4 -i`, then removing the offending commits. On a branch, preferably. It is the easy way. But I would only do this if the whole problem is described in your question. Other details might me make rethink it. – Geeky Guy Aug 13 '13 at 19:58