Git Delete / Remove Last Commit From Local and Remote

Posted by Damodar Bashyal on May 23, 2014


This is the function I always forget and keep googling, So here are my finds to delete, revert or remove commits from local and remote.

For local i.e. if the commit is not pushed to remote:

git reset --soft HEAD~1

That will remove commit but files will be there with all the changes. Then you can fix issues and recommit. If you want to remove all changes and make your local to be exacly same as remote version then you can do hard reset as:

git reset --hard HEAD

But if you already pushed to remote then note down the last commit you want to go back and reset to that commit as:

git reset --hard f874683

Now you don't have commit with issue you want to remove / delete. Now all you need to do is force (-f or --force) push that branch to remote.

git push origin develop  -f

Then your previous commits then your current commit will be deleted. I tested this with our bitbucket repository and it worked fine. I am using git version 1.8.3.msysgit.0.

Let me know if you have better ideas.

not published on website

QR Code: Git Delete / Remove Last Commit From Local and Remote