-1

If I commit many changes, then I realize one of these commits is not correct. How could I remove just that commit. For example, below is the git log (with some arguments to make it easy to check), I commit 3 changes, and I want to remove the change from test2, also I want to remove the commit record from git log as well. How could I do that?

* 28b3c19 2014-10-21 | test3 [Xinyu Hou]
* 73ca35d 2014-10-21 | test2 [Xinyu Hou]
* 2785334 2014-10-21 | test1 [Xinyu Hou]
* 9729226 2014-10-21 | cleaned up .gitignore

I hope the result could be something like below

* 28b3c19 2014-10-21 | test3 [Xinyu Hou]
* 2785334 2014-10-21 | test1 [Xinyu Hou]
* 9729226 2014-10-21 | cleaned up .gitignore

I tried git revert, apart from the conflict, it doesn't seem to remove the commit record in log.

Thanks, Jerry

Jerry
  • 1,704
  • 5
  • 20
  • 40

3 Answers3

1

Try git rebase --interactive 2785334, then when the list of commits comes up, delete the one you want removed. Note that the commit you specify when you run git rebase --interactive must come before the one you want to remove. Also note that this might not work, and even if it does, it can be troublesome (if commits after the one you're removing have changes based off the one you removed, resolving those conflicts can be tough). Furthermore, note that messing with the commit history can be a problem if the repo is published (see warnings regarding git rebase in git manpages or just about every git tutorial out there).

Tonio
  • 1,516
  • 1
  • 15
  • 25
0

Messing with the git log is always a bad idea. You may not be able to achieve this, especially if you pushed your commits to remote.

Maybe some of these suggestions may help you How to delete a 'git commit' from LOG, like it had never existed

Community
  • 1
  • 1
Nico Wiedemann
  • 174
  • 2
  • 10
0

If this is already pushed to origin (aka Published History) you can use

git revert

If this is not yet pushed; then you can use interactive rebase and delete offending commit.

forvaidya
  • 3,041
  • 3
  • 26
  • 33