Git Reset vs Git Revert: Safe Ways to “U-Turn” for Developers

Git tutorial - IT technology blog
Git tutorial - IT technology blog

Reset and Revert: Two Different Paths to Fixing Mistakes

When I first started working, I once accidentally committed an .env file containing database passwords to the server. Panicked, I quickly searched and ran git reset --hard. The result? The commit disappeared from my machine, but the entire 5-person team couldn’t push code anymore due to a serious history mismatch. That was my first painful lesson in using the wrong troubleshooting tool.

Both of these commands help you return to a previous state, but how they handle commit history is vastly different:

  • Git Reset: Like using an eraser to remove entries from a diary. You completely delete commits as if they never existed.
  • Git Revert: Like writing a correction line: “The content above is wrong, please reverse it.” The old history remains, and you add a new commit to undo the changes.

Comparing Pros and Cons in Practice

1. Git Reset – A Risky “Time Machine”

Pros: Keeps the commit log extremely clean. If you commit by mistake on your local machine and haven’t pushed to the server, Reset is the fastest way to wipe the trail.

Cons: Extremely dangerous on shared branches like main or develop. Reset alters the project’s commit history. If colleagues have already pulled the old code, your Reset will cause heavy conflicts when they try to sync back.

2. Git Revert – The Professional Approach

Pros: Completely safe for team projects. It doesn’t delete any past data. This helps the team maintain a continuous history without ever encountering “non-fast-forward” errors.

Cons: Can make the commit list look cluttered. You’ll see commits like “Revert ‘Add login feature'”. If you have to revert 5-10 times consecutively, looking at the Git log can be exhausting.

Should You Choose Reset or Revert to Avoid Frustrating Your Team?

After years of managing projects ranging from 2 to 20 people, I’ve developed this rule of thumb:

  1. Use Git Reset when: You are coding alone on a personal branch (feature branch) and haven’t pushed to GitHub/GitLab. Additionally, Reset is very useful for squashing 5-6 minor commits into one large commit before creating a Pull Request.
  2. Use Git Revert when: The code is already on the server or others are working on the same branch. This is the most civilized way to handle errors without breaking everyone’s workflow.

In my current team of 8, I strictly forbid force push to main branches. Thanks to this discipline, the number of “emergency cases” due to history mismatches has dropped to zero over the past year.

Detailed Implementation Guide

Using Git Reset with 3 Levels

You need to clearly distinguish between these 3 flags to avoid accidental code loss.

–soft: Go Back but Keep Your Code

This command only moves the HEAD pointer; all the code you just wrote remains in the Staging Area. This is a great way to combine multiple small commits into one.

# Squash the last commit to edit the message
git reset --soft HEAD~1

–mixed (Default): Keep Code but Unstage It

This is the default mode if you don’t specify a flag. The code is still there but in an Unstaged state; you need to git add again if you want to commit further.

# Go back to the commit with hash abc1234
git reset abc1234

–hard: Wipe Everything Without a Trace – Be Careful!

This command deletes all changes and returns the source code exactly to the state of the specified commit. Don’t use it unless you’re sure you no longer need that code.

# Wipe everything to return to the cleanest state
git reset --hard HEAD

Using Git Revert

Revert is simpler and safer. You just need to find the faulty commit and command it to reverse.

# View history to get the hash
git log --oneline

# Reverse the faulty commit (e.g., e4f5g6h)
git revert e4f5g6h

Git will automatically open an editor for you to enter the reason for the revert. Just save it, and you’re done. If you encounter a conflict, handle it just like a regular code merge.

Emergency Tip: The Lifesaver Called Reflog

Accidentally ran git reset --hard and lost a full day’s work? Don’t despair just yet. Git has a secret mechanism called reflog that records every movement of the HEAD pointer.

Type this immediately:

git reflog

You’ll see a list of every action you’ve performed. Just find the hash right before you ran the Reset command, then Reset back to that hash, and your code will be resurrected. I’ve used this method to save many Juniors on my team when they accidentally “played with fire”.

Final takeaway: Reset for Local, Revert for Public. Master this mindset, and you’ll have full control over your source code and feel more confident in any troubleshooting situation.

Share: