Skip to main content

Command Palette

Search for a command to run...

Git Panic Button

Updated
โ€ข3 min read
Git Panic Button

How to Fix Mistakes with Revert & Reset Simply Explained

Slug: git-revert-vs-reset-guide


Have you ever pushed code to production, only to realize it caused a massive bug? ๐Ÿ˜ฑ Your heart starts racing, and you just want to "Undo" it immediately.

But here is the problem: You cannot just delete history in a shared team project. It will break everyone else's code.

In this guide, we will look at the two best ways to fix mistakes in Git: Git Revert (the safe way) and Git Reset (the powerful way).

Part 1: The Safe Way โ€“ Git Revert ๐Ÿ›ก๏ธ

Imagine you built a tower of blocks.

  • Deleting the top block is like git reset.

  • Adding a new block that says "Ignore the previous block" is like git revert.

git revert is safe because it doesn't delete history. Instead, it creates a new commit that does the exact opposite of the bad commit.

How to do it (Step-by-Step)

1. Find the bad commit ID

Run this command to see your history:

Bash

git log --oneline

You will see something like:

c3d5a1a Add breaking log (โŒ The bad one)

0a7b3c2 Initial commit (โœ… The good one)

2. Revert the bad commit

Bash

git revert c3d5a1a

Git will automatically calculate the opposite of your changes and create a new commit.

3. Check the result

Bash

git log --oneline

Result:

8b31a73 Revert "Add breaking log" (โœจ New fix)

c3d5a1a Add breaking log (Still there, but cancelled out!)

0a7b3c2 Initial commit

When to use Revert:

  • When the code is already public (pushed to GitHub/GitLab).

  • When working in a team.


Part 2: The Time Machine โ€“ Git Reset โฐ

git reset is different. It actually moves time backward. It forgets the bad commit ever happened. This is great for local mistakes, but dangerous for shared code.

There are three "modes" of resetting. Think of them like levels of forgetfulness:

1. Soft Reset (--soft)

"I want to undo the commit, but keep my work."

Bash

git reset --soft HEAD~1
  • What happens: The commit is gone, but your files are still there and "staged" (ready to be committed again).

  • Use case: You made a typo in the commit message.

2. Mixed Reset (--mixed)

"I want to undo the commit and unstage the files."

Bash

git reset --mixed HEAD~1
  • What happens: The commit is gone. Your files are safe in your folder, but they are not staged yet.

  • Use case: You accidentally added a file you didn't mean to include.

3. Hard Reset (--hard)

"I want to destroy everything." ๐Ÿ’ฃ

Bash

git reset --hard HEAD~1
  • What happens: The commit is gone. Your changes are gone. Everything goes back to how it was before.

  • Use case: You completely messed up and want a fresh start. Be careful!


Cheat Sheet: Revert vs. Reset

FeatureGit RevertGit Reset
What it doesAdds a new "undo" commitMoves history backward
Safe for teams?โœ… Yes, very safeโŒ No (unless local)
HistoryKeeps history intactRewrites/Deletes history
Best forFixing bugs in productionFixing local typos or cleanup

๐Ÿ’ก Pro Tip: The "Undo" Button for Git

If you accidentally did a git reset --hard and lost your work, don't panic! You can use the Reflog to find it:

Bash

git reflog
git reset --hard <commit-id>

Git keeps a secret diary of every move you make, even the mistakes!