Git

How to Undo Things in Git (Without Panic)

Git offers several powerful commands like restore, reset, and revert to safely undo mistakes—whether you deleted a file, made a bad commit, or pushed too soon—so there’s no need to panic.

April 1, 2025

We’ve all been there: you delete the wrong file, commit the wrong changes, or hit "push" too soon.

Don’t worry—Git has your back.

It comes with built-in ways to undo mistakes, and once you understand a few basic commands, you’ll feel much more in control.

Let’s walk through how to fix common Git goofs—with zero panic.

“Oops, I Deleted a File I Didn't Mean To”

If you accidentally delete a file but haven’t committed yet:

git restore filename.txt

This brings the file back from your last commit.

Think of it like “undo delete” in a text editor.

“I Staged Something I Didn’t Want To”

If you ran git add too soon:

git restore --staged filename.txt

This unstages the file but keeps your changes, so you can fix them before committing.

Like taking an item out of your shopping cart before checkout.

“I Committed the Wrong Changes”

If you already ran git commit but haven’t pushed yet:

git reset --soft HEAD~1

This undoes the last commit but keeps all your changes staged, so you can make adjustments and recommit.

Think of it like saying, “Actually, let me rephrase that...”

“I Want to Completely Remove My Last Commit”

If you want to erase the last commit and your changes:

git reset --hard HEAD~1

Warning: this permanently deletes the commit and the changes. Only use it if you're 100% sure!

Like using “Clear All” instead of “Undo.”

“I Already Pushed Something and Regret It”

If your changes are already on GitHub or a remote repo, use:

This creates a new commit that reverses the old one, keeping history clean and intact.

It’s like adding an “undo” entry to your journal instead of tearing out the page.

Key Takeaway

Git makes it easy to fix mistakes—as long as you know which command fits the situation.

Whether you staged too soon, committed too early, or deleted the wrong file, there’s usually a safe way out.

So next time something goes wrong, take a breath, and remember: Git has an undo button (actually, several).