1

I have 3 commits: a, b & c

I need to remove the changes introduced by b, such that just a & c are left

What I've done so far is create a new branch from a, and then cherry picked in c

However, when I merged with the master it merged back in b

How do I avoid this merge?

DJ180
  • 18,724
  • 21
  • 66
  • 117
  • This really depends on whether you have pushed your commits. If they're only local, you can do many things. However, if they've been pushed, you'll want to stick with `git revert` – mwarsco Jul 10 '14 at 16:10
  • Yes, they are all pushed remotely unfortuantely – DJ180 Jul 10 '14 at 16:33
  • Why is this post downgraded without any comment to go with it? – DJ180 Jul 10 '14 at 16:34
  • Possible duplicate of [I need to pop up and trash away a "middle" commit in my master branch. How can I do it?](http://stackoverflow.com/questions/5757187/i-need-to-pop-up-and-trash-away-a-middle-commit-in-my-master-branch-how-can-i) – Waldir Leoncio Mar 31 '17 at 09:15

1 Answers1

7

If you want to remove B in A --> B --> C, do :

git rebase -i HEAD~ A

You will get the interactive rebase text. Just remove the line representing B and continue the rebase and B will be removed.

Note that this will change your history, and should generally be done only when you have not pushed these commits to remote.

If pushed, do git revert B to make a new commit B' which removes the changes done by.

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • `git revert B` gives error "Could not revert" for Git version 2.15 on Windows 8. I created a sample repo, and created 7 commits in it on master branch, for example, say, C1, C2, C3... C7. Now when i give `git revert C6` it throws error:- `$ git revert 57e70d8 error: could not revert 57e70d8... C4 hint: after resolving the conflicts, mark the corrected paths hint: with 'git add ' or 'git rm ' hint: and commit the result with 'git commit' ` Did something change in latest versions of Git? Any pointers what am i doing wrong? @DJ180 @manojlds – Anuroop Nov 08 '17 at 08:11