Sometimes we mess up the last commit badly and a simple revert isn’t enough. We want to wipe it off the face of the earth (locally and remotely) as if it never happened.
Danger: Destructive
This method deletes changes. If you work in a team, make sure no one has pulled your changes before doing this, or you will have problems.
1. Fix message or forgotten file (Quick Option)
If you just made a typo in the message or forgot to add a file, you don’t need to reset. Add the changes and “amend” the commit. This replaces the last commit with a new one fixing the mistake.
git add .
git commit --amendgit add .
git commit --amendWhen you run this, your default text editor (Vim, Nano, VS Code…) will open with the previous commit message.
- Edit the message if you like.
- Save and close the file (in Vim:
:wq). - Git will detect you are done and create the new commit replacing the old one.
2. Go back without losing work (Local)
We use reset --soft to undo the commit but keep your changes staged. This way you can fix what was wrong, add what was missing, and commit again.
git reset --soft HEAD~1git reset --soft HEAD~13. Overwrite history (Remote)
Now your local repo is “in the past” but GitHub is still “in the future”. If you try a normal push, git will reject you. You need to force write so the remote accepts your old version as the “absolute truth”.
git push origin +maingit push origin +mainNote: The
+symbol before the branch name (+main) is shorthand syntax for--force. It does the same thing but makes you feel more like a hacker.
Dangerous Alternative: Hard Reset (The Nuclear Option)
If git reset --soft is like backspacing, git reset --hard is like burning the paper and starting fresh. It’s the nuclear option for when you don’t want to recover anything from your last commit.
Extreme Danger
This command permanently deletes all changes in your uncommitted files. There is no (easy) going back. Use only if you are 100% sure your recent work is trash.
1. Erase all traces (Local)
This moves the pointer back and syncs your working directory to that old state.
git reset --hard HEAD~1git reset --hard HEAD~12. Force history (Remote)
Just like with soft reset, the remote will reject this because you rewrote the past.
git push origin +maingit push origin +main