Want to remove/delete your last commit? Here is how.

1. To remove a commit done locally:

You made a commit and then realized you want to remove it. But, you still want to keep your changes. This is achieved by:

$ git reset --soft HEAD^

HEAD^ means go back one commit from where HEAD is now. This will get you to the state just before the last commit. If you do a git status you will see that your changes are there just as they where before you staged them.

2. To remove a commit you have already pushed:

This is slightly tricky. There are safe and unsafe ways of doing this. Some of these unsafe ways involve changing the history of the repo and will create problems for other developers working with you in the same repo. I do not recommend those methods.

The safest way of removing a commit from remote is to revert the bad commit. Find the commit hash and:

$ git revert <commit-hash>

This creates a new commit that undos the changes made in the bad commit. Now push this to remote and you are good to go.

Subscribe via RSS