Git Revert Explained — Difference Between Reset and Revert

In the previous blog, we looked at git reset and saw how --soft, --mixed, and --hard behave.
Now let’s move on to git revert, another important command for undoing changes.

We’ll cover:

  • What git revert does.
  • How it differs from git reset.
  • When to use revert vs reset.
  • Real-world examples.

Project Setup

Let’s assume a repo with four commits:

git log --oneline
e91a2c4 (HEAD -> main) 4th commit (file4.txt)
a3f8b71 3rd commit (file3.txt)
62c559b 2nd commit (file2.txt)
1f92b10 1st commit (file1.txt)

📂 Files currently:

file1.txt
file2.txt
file3.txt
file4.txt

Using git revert

Suppose we realize the 4th commit (adding file4.txt) was a mistake.

Step 1: Run revert

git revert HEAD

Git will open your editor for a commit message (default: Revert "4th commit"). Save and exit.

Step 2: Check the log

git log --oneline
e1b2f9a (HEAD -> main) Revert "4th commit"
e91a2c4 4th commit
a3f8b71 3rd commit
62c559b 2nd commit
1f92b10 1st commit

📂 Files now:

file1.txt
file2.txt
file3.txt

✅ A new commit was added that cancels the changes introduced in the 4th commit.


Reverting an Older Commit

You can also revert older commits.

For example, revert the 2nd commit:

git revert HEAD~2

This creates a new commit that removes file2.txt.

👉 Files after revert:

file1.txt
file3.txt

Difference Between Reset and Revert

Featuregit resetgit revert
What it doesMoves the branch pointer backCreates a new commit that undoes previous commit(s)
Commit historyOld commits can disappear (rewrites history)History remains intact (new commit added)
Safe for shared repos?❌ Not safe after pushing✅ Safe (keeps history clean)
Use caseFix local mistakes before pushingUndo a commit that’s already shared

When to Use What?

  • Use git reset → If the commits are local only, not pushed to remote.
  • Use git revert → If the commits are already pushed and shared with a team.

Summary

  • git reset moves the pointer back, can rewrite history.
  • git revert creates a new commit that undoes previous changes, preserving history.
  • In team projects, always prefer revert for safety.

Keep Learning 🚀

👉 Subscribe to Learning Ocean – Subscribers get coupon codes for my courses, early access to blogs and courses, and even exclusive YouTube videos.

👉 My YouTube Channel – More videos, more fun, and lots of learning!

👉 📺 Watch this topic in video form

Stay curious, keep coding, and let’s make learning fun together! 🎉