Undoing Mistakes in Git: reset, revert, and checkout Explained
Let’s be honest — if you work with Git long enough, you will mess something up.
Maybe you:
- Committed the wrong file
- Pushed a broken configuration
- Deleted something important
- Or just want to “go back” to a previous state
And that’s when panic kicks in.
“Should I use reset? Revert? Checkout? Am I about to break everything?”
Good news: Git gives you multiple ways to undo mistakes — and once you understand them, they’re not scary at all.
Let’s break down the three most important commands: reset, revert, and checkout — in a way that actually makes sense.
First: Why Git Makes Undoing Possible
Git is built on snapshots.
Every commit represents a complete state of your project, not just changes. That means:
You’re never really “losing” work — you’re just moving pointers.
That’s why undoing mistakes is usually safe — if you choose the right command.
git reset: Rewriting History (Handle With Care)
git reset moves your branch pointer backward.
Basic example:
git reset --hard HEAD~1This command:
- Moves the current branch back one commit
- Updates your working directory
- Deletes the latest commit (from your visible history)
When to Use reset
Use reset when:
- You made a mistake in a local commit
- You haven’t pushed changes yet
- You want to completely redo recent work
Real DevOps Example
You accidentally commit secrets into a config file locally:
git commit -m "Add config with credentials"Before pushing, you realize the mistake.
You can:
git reset --hard HEAD~1Now the commit is gone from history (locally).
Important Warning
Never use git reset on shared or pushed commits.
Why?
- It rewrites history
- It breaks other people’s repositories
- It creates confusion and conflicts
git revert: The Safe Undo for Shared History
git revert doesn’t delete history.
Instead, it creates a new commit that undoes a previous one.
Example:
git revert HEADThis:
- Keeps the original commit
- Adds a new commit that reverses its changes
When to Use revert
Use revert when:
- Changes are already pushed
- You’re working in a shared repository
- You want a safe, traceable rollback
Real DevOps Example
A Kubernetes deployment breaks production because of this change:
replicas: 0The commit is already merged into main.
Get TheVega.AI’s stories in your inbox
Join Medium for free to get updates from this writer.
Instead of rewriting history, you run:
git revert <commit-hash>This creates a new commit restoring the previous state.
Your deployment stabilizes — without messing up history.
Why DevOps Teams Prefer Revert
- Keeps audit trails intact
- Works safely with CI/CD pipelines
- Avoids breaking shared branches
In production environments, revert is usually the correct choice.
git checkout: Time Travel Without Changing History
git checkout lets you:
- Switch branches
- Or explore previous commits
Example:
git checkout <commit-hash>This puts you in a detached HEAD state.
You’re not changing history — just viewing it.
When to Use checkout
Use it when:
- You want to inspect old code
- You need to debug past states
- You want to test something temporarily
Real DevOps Example
A service worked perfectly last week but fails now.
You can:
git checkout <old-commit>Now you:
- Run the old version locally
- Compare behavior
- Identify what changed
No risk. No history changes.
Quick Comparison: Which One Should You Use?
| Command | What It Does | Safe for Shared Repo? | Use Case |
| ---------- | ------------------------------- | --------------------- | ------------------- |
| reset | Moves branch pointer (rewrites) | ❌ No | Fix local mistakes |
| revert | Adds a new undo commit | ✅ Yes | Undo pushed changes |
| checkout | Switches state / explores | ✅ Yes | Debugging / viewing |A Simple Rule to Remember
If you’re unsure, follow this:
- Local mistake? →
reset - Shared/production mistake? →
revert - Just exploring? →
checkout
This one rule avoids most Git disasters.
Why This Matters in DevOps
In DevOps, mistakes can:
- Break deployments
- Take down services
- Misconfigure infrastructure
Knowing how to undo safely means:
- Faster incident recovery
- Less panic during outages
- More confidence in making changes
Git doesn’t just help you move forward — it helps you recover when things go wrong.
Final Thoughts
Mistakes in Git are inevitable.
What matters is:
- Knowing the right tool
- Understanding the impact
- Acting confidently under pressure
reset, revert, and checkout aren’t just commands — they’re your safety net.
Once you understand them, Git stops feeling risky and starts feeling reliable.
Over to You
What’s the biggest Git mistake you’ve ever made — and how did you fix it?
Did reset, revert, or checkout save you… or make things worse?
Share your story below








