Knowledge - Details

Delete last commit from remote Git repo
Date Added: 1/13/2022

If you have changed some code and committed the changes to your local repo, use this command to permanently delete the most recent commit from your local repo history and bring your local code back to what it was before that unwanted commit. You will lose all those changes!

git reset --hard HEAD~1

Next, if you have also pushed that unwanted commit to a remote repo and want to delete the commit from that remote repo as well, then use this command. It will permanently overwrite the remote repo with your local one, losing any changes made to the remote repo that aren't already in your local repo.

git push -f

Note: A safer way is to use the "revert" command, which creates a new commit that reverses the changes from the unwanted commit. That has the same effective result (setting your local code back to what you want and setting the head of the repo to match), except it leaves the unwanted commit in the history of your repo for future reference. That history may be useful if you ever want to refer to that code again, or it may be ugly or space-consuming if you don't want to keep it.

Back to List