13

I have made a mistake in one of the commits. Now I want to completely delete this commit, so it looks like it has never existed. I don't want to see this in log.

I have tried all tips from this question ("How to delete a 'git commit'"), but I can see the commit in the log. How I can completely delete it?

-- Edit --

Ok, I do not give the completely information. Kevin Ballard are correct.

By now, I do not push this commit, it's only in my machine. The ouah answer work, the command

git log

will not show, but what the command

git reset --hard HEAD^

do is "chekout last commit and change the the branch to this", so I continue seeing that commit with a graph program like SmartGit.

--Edit 2--

No, this is a SmartGit bug!!!! The commit really disappear. I have to close the windows of log and than open again. The commit is no more there.

Community
  • 1
  • 1
Rodrigo
  • 11,909
  • 23
  • 68
  • 101
  • I might be mistaken, but I think that you can't. – zneak Jan 17 '12 at 20:50
  • @zneak: You can, but Rodrigo needs to give more information. Has this commit been pushed anywhere? Do you care about erasing all information about this commit and its files from disk, or is it ok to just make sure it doesn't show up in `git log` (and can be garbage-collected naturally at some later point in time)? – Lily Ballard Jan 17 '12 at 20:51
  • Try the tips in this http://stackoverflow.com/questions/927358/git-undo-last-commit – Shraddha Jan 17 '12 at 20:52

2 Answers2

16

If it is the last commit

git reset --hard HEAD^

if it is not the last commit

git rebase -i commit_hash^

an editor will open, delete the whole line with the commit, save and quit.

Note that rewriting history or rebasing if the branch has already been pushed is usually a bad idea and you may prefer to use

git revert commit_hash

that will add a new commit that reverts the commit commit_hash.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • 3
    If you're trying to remove a single commit, you can also just use `git rebase --onto $SHA1^ $SHA1` (where `$SHA1` is the commit to delete). Also, this come with the caveat that it won't reproduce merges. You can give it the `-p` flag to try and reproduce merges, but it may not work properly if the commit you deleted has an impact on any merge resolutions. Also note that mixing `-p` with `-i` is a bad idea (see the BUGS section in the git-rebase manpage). – Lily Ballard Jan 17 '12 at 20:54
  • @ouah you should probably mention that this if the branch has been pushed, then this is a BAD IDEA. – Richard Jan 17 '12 at 21:02
  • git revert. is the best solution =) – Arthur Neves Aug 06 '12 at 16:26
  • and if you want this to the remote branch, then do "git push -f" after you do "git reset HEAD^ --hard" – Vignesh PT Jul 19 '14 at 19:30
5

This command (beware, it would rewrite history):

git rebase --onto commitHash^ commitHash

(@ouah's solution didn't work for me, and instead Lily's did, but his solution shouldn't be a comment, it should be an answer like this.)

knocte
  • 16,941
  • 11
  • 79
  • 125