Knowledge - Details
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.
Disclaimer: Everything on this website is written for my own use. I disclaim any guarantees that the procedures and advice listed here are accurate, safe, or beneficial for anyone else. If you attempt to follow any procedures or advice shared here, you do it at your own risk. Part of IT work is knowing how to recover from problems.