Git Undo
Estimated reading: 3 minutes 6 views

🔁 What is git revert in Git?

A Simple Guide with FAQ


🧾 What is git revert?

git revert is a command used to undo a commit by creating a new commit that reverses the changes made in a previous one.
Unlike git reset, it does not delete history, which makes it a safer option — especially when working with others.

In simple terms:
Undo the changes from an old commit
Keep the history clean and intact


💡 Why Do We Use git revert?

We use git revert when:

  • We want to undo a mistake from a previous commit.
  • We are working in a shared repository and don’t want to break history.
  • We want to keep a full, traceable record of all changes — even the ones we reverted.

📌 Significance of git revert

  • Safe for team collaboration
    It doesn’t change commit history, so it’s great for shared projects.
  • Keeps your commit history clean
    Shows clearly what was done — and what was undone.
  • No loss of work
    Unlike reset --hard, it doesn’t delete anything permanently.
  • Supports rollback without drama
    You can undo one commit without affecting others.

🛠️ How to Use git revert

🔹 Revert a Specific Commit

git revert <commit-id>
  • This opens a text editor for the commit message.
  • You can edit the message or leave it as-is.
  • To save and exit:
    • Type your message (optional)
    • Press Esc
    • Type :wq! and press Enter

This creates a new commit that undoes the selected commit’s changes.


🔹 Add a Custom Commit Message During Revert

git revert <commit-id> -m "Reverting bug introduced in login system"

You can also use the default message and edit it in the editor.


⚠️ Example:

Let’s say the commit ID is abc123. To revert it:

git revert abc123

This will open the commit message editor. Then:

  • Press i to insert text (optional)
  • Write your custom message (or leave the default)
  • Press Esc
  • Type :wq! and hit Enter to save and exit

❓ FAQ – Frequently Asked Questions

Q: What’s the difference between git revert and git reset?

A:

  • git revert adds a new commit that undoes the changes — safe and clean.
  • git reset moves your branch back in history — can rewrite history, which may cause issues in shared repos.

Q: Does git revert delete the original commit?

A: No. It creates a new commit that reverses the changes, but the original commit still exists in the history.


Q: Can I revert multiple commits at once?

A: Yes. You can use:

git revert HEAD~2..HEAD

This reverts the last 2 commits. You’ll be asked to confirm each one.


Q: What does :wq! mean?

A: It’s a command used in the text editor (like Vim):

  • w = write (save)
  • q = quit
  • ! = force the action

Together, :wq! means “save and exit the editor.”


Q: Can I cancel a git revert if I haven’t committed yet?

A: Yes. Just run:

git revert --abort

This cancels the revert operation.

Leave a Reply

Your email address will not be published. Required fields are marked *

Share this Doc

Git Revert

Or copy link

CONTENTS
Scroll to Top