Explore Courses Blog Tutorials Interview Questions
+3 votes
in DevOps and Agile by (19.4k points)
edited by

I would like to know how to delete a commit.

By delete, I mean it is as if I didn't make that commit, and when I do a push in the future, my changes will not push to the remote branch.

I read git help, and I think the command I should use is git reset --hard HEAD. Is this correct?

1 Answer

+3 votes
by (27.5k points)
edited by

Case 1: Assuming you are sitting on that commit, then this command will nuke the last commit

git reset --hard HEAD~1

Case 2: Take a look at the output of git log, find the commit id of the commit you want to delete, and then do this:

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

Case 3: If you already pushed it, then you need to do a force push to get rid of it.

git push origin HEAD --force

For more commands like this please go through the following tutorial that will help you understand the git


by (19.4k points)
Thanks, this worked to me and is there any problem in future if we use --force option.
by (29.3k points)
--force option will force push the changes (and overwrite the previous push in this case).

Browse Categories