05 Feb 25
How To Uncommit In Git?
Let’s be real — if you’ve spent any real time using Git, you’re gonna mess something up sooner or later. Doesn’t matter if you’re flying solo or working with a whole team. It happens. You commit something too soon, forget a file, mess up a message, or push something that really shouldn’t be there.
Git’s brilliant, but it’s also forgiving—if you know what you’re doing.
So, I want to walk you through how I typically approach “uncommitting” in Git, based on what I’ve seen work over the years. This isn’t theory—it’s the stuff you actually run into on real projects.
First—What Exactly Is a Commit?
Quick recap, just to level set.
A commit is basically a snapshot of your code at a certain point. Git saves the files, the changes, and slaps on a unique hash ID so you can always come back to it. It’s like saving a versioned backup.
But sometimes that snapshot isn’t quite what you wanted. Maybe you included some debug code. Maybe you left out a file. Maybe you were up too late and hit commit by accident (guilty as charged).
That’s where uncommitting comes in handy.
Option 1: Undo The Most Recent Commit
Happens all the time. You just hit commit and immediately go, “Ah crap.”
Two options here, depending on whether you want to keep your changes or blow them away.
git reset --soft HEAD~1
-
Keeps your changes staged (in the index), but removes the commit.
-
Lets you fix your files or message, and re-commit properly.
When to use it:
You realised seconds later you needed to add one more file or fix your message.
git reset --hard HEAD~1
-
Wipes out the commit and the changes.
-
Dangerous. Permanent. No take-backs.
When to use it:
You committed garbage you never want to see again. But double-check first.
Heads up:
I’ve seen folks nuke full afternoons of work by running --hard
a little too casually. If you’re unsure, don’t use it. Or at least run git reflog
first (more on that in a minute).
Option 2: Amend The Last Commit
Honestly, this one gets used a lot more than people admit.
git commit --amend
-
Allows you to fix the commit message.
-
Add/remove files if you’ve staged them first.
-
The commit hash technically changes, but you stay tidy.
When to use it:
You just forgot to stage one extra file or you want a better message.
I remember one time in 2022, we were working on a fintech app with a tight audit trail requirement. Amending kept the history clean before the code review, and nobody got yelled at.
Option 3: Revert A Commit (Without Messing Up History)
Now this is your best friend when you’ve already pushed your commits, and others are working off it.
git revert <commit>
-
Creates a new commit that undoes the changes from a previous commit.
-
Keeps history fully intact.
-
Completely safe for shared repos.
When to use it:
You pushed something dumb, but the whole team has already pulled. You need to reverse the changes without breaking everyone’s clone.
This one’s a lifesaver in team environments.
Advanced Tricks: Recovering or Partially Uncommitting
Now, let’s talk about the “oh no” situations.
1) Recover Lost Commits with Reflog
Ever nuked a commit and instantly regretted it? Git’s got your back (sometimes).
git reflog
-
Shows every movement of HEAD.
-
Lets you see even deleted commits.
-
Use
git checkout <commit-hash>
to restore the lost commit.
When to use it:
You ran git reset --hard
when you shouldn’t have. Found out 10 minutes later. Panic sets in. Reflog saves you.
2) Unstage Specific Files
Sometimes you only need to pull back part of what you staged.
git reset <file>
-
Unstages the file but keeps your work intact.
-
Great for when you staged too aggressively.
git checkout -- <file>
-
Reverts the file to its last committed state.
-
Destroys uncommitted changes in that file.
When to use it:
You’re working on five files, only meant to commit three. Easy fix.
Quick Best Practices From Experience
Here’s the stuff I tell junior devs all the time:
-
Be paranoid with
git reset --hard
. It’s powerful. And brutal. -
Branch for experiments. Keeps your main safe while you tinker.
-
Talk to your team before rewriting history. Especially on shared branches.
-
Always check
git status
andgit diff
before you commit. Saves so many headaches.
Look, Git’s like a loaded weapon. It’s incredible—but you need to respect it.
The Bottom Line
Uncommitting isn’t scary once you know your tools.
Whether you’re fixing a tiny message typo or rolling back a giant accidental merge, Git gives you options. The key is knowing which option fits your situation, and having a little healthy caution.
Now—what’s the worst Git mistake you’ve had to clean up? (be honest.)