-3

I made some commits to my master git branch and put some buggy code on, which I want to get rid of.

Commit 1
Commit 2
Commit 3

I want to keep commit 2, and get rid of the changes made in commit 1 and 3. Commit 2 and Commits 1,3 have no intersection. Additionally it's worth pointing out that all the changes from commit 1 and 3 are confined to a few files in a specific directory, which commit 2 did not touch either.

How do I do this with git?

appleLover
  • 14,835
  • 9
  • 33
  • 50

1 Answers1

0

You should use interactive git rebase:

git rebase -i HEAD~3

You will see a list of your 3 commits. Remove the line with the commit that is not needed and save and exit the editor. Git will perform the rebase operation and remove the commit

If you have pushed your chanes to the upstream remote you will need to perform forced push, i.e. forcefully overwrite the history. Remember though that there is only one rule in Git - do not overwrite published history. This is mostly important when you are collaborating with many people on some project. And in this case if you pushed a buggy commit, i would recommend to either use the

git revert commitID 

or just fix the bug and commit the fix on top of the commit3

Eugene Sajine
  • 8,104
  • 3
  • 23
  • 28