Article General May 17, 2026 2 min read

How to Undo a Git Commit

Pax Durand

Technical Writer

May 17, 2026
2 min read

We have all been there. You hit enter on a Git commit, and immediately realize something is wrong. Maybe you spotted a typo, forgot to include a file, or committed to the wrong branch entirely.

The good news is that undoing a commit is incredibly simple. The right approach depends entirely on two questions: Have you pushed it to GitHub yet? And do you want to keep your code?

Scenario 1: The Commit is Only on Your Computer (Not Pushed Yet)

If you haven’t pushed your work to a remote server yet, you can safely rewrite your history. Nobody else will ever know.

If you want to keep your changes and just fix the commit:

This is the most common safety net. It undoes the commit but leaves your files modified and staged, exactly how they were right before you committed.

git reset --soft HEAD~1

If you just want to fix a typo in your last commit message:

Don’t bother undoing the whole thing. You can overwrite the last message instantly with this command:

git commit --amend -m "Your new and improved commit message"

If you want to completely destroy the commit and the code:

Use this with caution. If you want to throw away the commit and erase all the code modifications you just made, use a hard reset. This cannot be easily undone.

git reset --hard HEAD~1

Scenario 2: You Already Pushed the Commit to a Shared Branch

If your commit is already live on a remote server like GitHub or GitLab, changing history can break things for your team. You have two ways to handle this.

The Safe Team Player Approach (Revert)

Instead of erasing history, this command creates a brand new commit that does the exact opposite of your bad commit. It is clean, safe for teams, and keeps the historical record straight.

git revert HEAD
git push origin <your-branch-name>

The Solo Developer Approach (Force Push)

If you are working alone on a feature branch and absolutely want to wipe the mistake from existence, you can do a hard reset locally and force the server to accept it.

git reset --hard HEAD~1
git push origin <your-branch-name> --force-with-lease

Tip: Always use --force-with-lease instead of a plain --force. It protects you from accidentally overwriting a teammate’s work if they pushed something while you weren’t looking.

Leave a Reply

Your email address will not be published. Required fields are marked *