I honestly thought I’d seen every way a repo could break until a junior dev walked into my office last Tuesday. He’d accidentally merged a half-finished experiment into the production branch, panicked, and tried to delete the .git folder to “start over.” If you’ve ever felt that cold sweat after a bad commit, this guide is for you. We’re going to talk about how to Rewrite Git History with the confidence of someone who knows exactly where the “undo” button is.
In 14 years of WordPress development, I’ve learned that Git isn’t just a backup tool—it’s a time machine. But like any time machine, if you don’t understand the mechanics, you’ll end up in a timeline where your site is down and your coffee is cold.
Understanding the Three Trees of Git
Before you try to Rewrite Git History, you have to understand where your code actually lives. Git manages three different “trees” or states:
- The Working Directory: Your actual files on your laptop.
- The Index (Staging Area): The “waiting room” for your next commit.
- The Repository: The permanent snapshots (commits) Git has recorded.
Most “undo” operations are just moving data between these three areas. If you understand this, you stop guessing and start debugging. For a deeper dive into modern workflows, check out my take on Pro Block Theme Development Workflows.
The Senior Dev’s Toolbox: Git Reset
When you need to undo a local mistake, git reset is your best friend. It comes in three flavors, and choosing the wrong one is usually why things get messy.
1. Git Reset –Soft (The Surgeon’s Knife)
Use --soft when you just want to “un-commit.” It moves the branch pointer back, but keeps your changes staged in the index. This is perfect for fixing a typo in a commit message or combining multiple small commits into one clean one.
# Move back one commit, keep changes staged
git reset --soft HEAD~1
2. Git Reset –Mixed (The Default)
This is the default mode. It undoes the commit and the staging. Your changes are still there in your files, but they aren’t “added” to Git yet. I use this when I realize I staged too many files and need to be more selective.
3. Git Reset –Hard (The Nuclear Option)
This wipes everything. It matches your working directory and index exactly to the previous commit. Caution: Any work you haven’t committed is gone forever. I only use this when a local experiment has gone completely sideways.
How to Rewrite Git History for Pushed Commits
Here is a cardinal rule: never use reset on commits you’ve already pushed to a shared server. If you do, you’ll break the history for everyone else on your team. Instead, use git revert. It creates a *new* commit that does the exact opposite of the bad commit.
# Revert the very last commit safely
git revert HEAD
It’s a bit noisier in the log, but it’s the mature way to handle mistakes in a collaborative environment. For more technical details, the official Git documentation covers the nuances of revert vs. reset.
The “Oh Crap” Button: Git Reflog
What if you ran git reset --hard and realized you deleted the wrong thing? In most tools, you’d be out of luck. But Git keeps a secret log of every time your HEAD pointer moves. This is the reflog.
# See everything you've done in the last 30 days
git reflog
You’ll see a list of SHA-1 hashes. Find the one from right before you messed up, and you can reset back to it. It’s saved my skin more times than I care to admit.
Look, if this Rewrite Git History stuff is eating up your dev hours, let me handle it. I’ve been wrestling with WordPress since the 4.x days, and I’ve seen every race condition and broken merge imaginable.
Final Takeaway: Whiteboard Before You Type
The most important tool in my kit isn’t a command—it’s a piece of paper. Before you try to Rewrite Git History, draw the current state and the desired state. If you can’t draw it, you don’t understand it yet. Take a breath, check your trees, and use the right tool for the job. Git is remarkably forgiving if you stop treating it like a black box and start treating it like the filesystem snapshot engine it is.
“},excerpt:{raw: