4

I have a git repo with many commits. Lets say that we have these commits from past to present:

  1. small changes
  2. big feature addition
  3. small changes
  4. small changes

Now we have determined that for business reasons the big feature should be removed, but I want to keep the changes made since.

How best can I do that?

Rich Bradshaw
  • 71,795
  • 44
  • 182
  • 241
  • I usually use "git rebase -i HEAD~4" for this, delete the line with the commit not wanted, and there you go. – asjo Feb 11 '15 at 21:16
  • Rich - http://stackoverflow.com/questions/7099833/how-to-revert-a-merge-commit-thats-already-pushed-to-remote-branch – Andrew C Feb 11 '15 at 22:54

2 Answers2

6

git-rebase is overkill for that, and you lose history.

Use this instead:

$ git revert <hash of commit 2>

Your history will then show you had it committed a feature, then removed it later.

Makoto
  • 104,088
  • 27
  • 192
  • 230
underrun
  • 6,713
  • 2
  • 41
  • 53
  • I tried this but it's actually a merge commit - the hash doesn't work, it asks for a numerical value, but I'm unsure what value… – Rich Bradshaw Feb 11 '15 at 21:24
  • 1
    @RichBradshaw when trying to revert a merge commit, you need to tell `git revert` based on which parent it needs to evaluate the patch. If you want to revert the merged in changes you will have to use `git revert -m 1 `. – Sascha Wolf Feb 12 '15 at 08:38
  • 1
    And in general I would suggest taking a look at the [documentation of `git revert`](http://git-scm.com/docs/git-revert). – Sascha Wolf Feb 12 '15 at 09:02
0

Perform an interactive rebase:

$ 'git rebase -i HEAD~4'

then delete the one you don't want from the list. See this answer: https://stackoverflow.com/a/13061212/1024740

Community
  • 1
  • 1
Rob Latham
  • 5,085
  • 3
  • 27
  • 44