Sitemap
Press enter or click to view image in full size
When Git chaos hits, knowing the right command changes everything.

Undoing Mistakes in Git: , , and Explained

4 min readMar 27, 2026

--

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: , , and — 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.

: Rewriting History (Handle With Care)

moves your branch pointer backward.

Basic example:

git reset --hard HEAD~1

This command:

  • Moves the current branch back one commit
  • Updates your working directory
  • Deletes the latest commit (from your visible history)

When to Use

Use 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~1

Now the commit is gone from history (locally).

Important Warning

Never use on shared or pushed commits.

Why?

  • It rewrites history
  • It breaks other people’s repositories
  • It creates confusion and conflicts

: The Safe Undo for Shared History

doesn’t delete history.

Instead, it creates a new commit that undoes a previous one.

Example:

git revert HEAD

This:

  • Keeps the original commit
  • Adds a new commit that reverses its changes

When to Use

Use 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: 0

The commit is already merged into .

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.

Press enter or click to view image in full size
Reset, revert, or checkout — choose wisely, recover confidently.

: Time Travel Without Changing History

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

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? →
  • Shared/production mistake? →
  • Just exploring? →

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

, , and 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 , , or save you… or make things worse?

Share your story below

--

--

TheVega.AI
TheVega.AI

Written by TheVega.AI

DevOps Engineer passionate about teaching and sharing knowledge. Writing about DevOps, SRE, Linux, Kubernetes, and computer science behind modern infrastructure