tags:

views:

1307

answers:

4

Hi,

I would like to know how to delete a 'git commit', by delete, i mean 'it is as if I did not do that commmit and when i do a 'git push' in future, my changes will not push to the remote branch.

I read git help, I think the command i should use is 'git reset --hard HEAD'. Is that correct?

Thank you.

+11  A: 

Assuming you are sitting on that commit, then this command will wack it...

git reset --hard HEAD~1

The HEAD~1 means the commit before head.

Or, you could look at the output of git log, find the commit id of the commit you want to back up to, and then do this:

git reset --hard <sha1-commit-id>


If you already pushed it, you will need to do a force push to get rid of it...

git push origin HEAD --force

However, if others may have pulled it, then you would be better off starting a new branch. Because when they pull, it will just merge it into their work, and you will get it pushed back up again.

If you already pushed, it may be better to use git revert, to create a "mirror image" commit that will undo the changes. However, both commits will both be in the log.


FYI -- git reset --hard HEAD is great if you want to get rid of WORK IN PROGRESS. It will reset you back to the most recent commit, and erase all the changes in your working tree and index.

gahooa
`HEAD~1` or just `HEAD^`. If you pushed, you should use `git revert` instead.
Jakub Narębski
Thank you for all the answers.
hap497
+4  A: 

Another possibility is one of my personal favorite commands:

git rebase -i <commit>~1

This will start the rebase in interactive mode -i at the point just before the commit you want to whack. The editor will start up listing all of the commits since then. Delete the line containing the commit you want to obliterate and save the file. Rebase will do the rest of the work, deleting only that commit, and replaying all of the others back into the log.

1800 INFORMATION
+7  A: 

If you have not yet pushed the commit anywhere, you can use git rebase -i to remove that commit. First, find out how far back that commit is (approximately). Then do:

git rebase -i HEAD~10

The ~10 means rebase the last 10 commits. Then, you can edit the file that Git presents to you to delete the offending commit. On saving that file, Git will then rewrite all the following commits as if the one you deleted didn't exist.

The Git Book has a good section on rebasing with pictures and examples.

Be careful with this though, because if you change something that you have pushed elsewhere, another approach will be needed.

Greg Hewgill
+1  A: 

If you didn't publish changes, to remove latest commit, you can do

$ git reset --hard HEAD^

(note that this would also remove all uncommitted changes; use with care).

If you already published to-be-deleted commit, use git revert

$ git revert HEAD
Jakub Narębski