Trending News

Blog

How To Revert A Commit In Git: Step-By-Step Commands And Best Practices
Blog

How To Revert A Commit In Git: Step-By-Step Commands And Best Practices 

So you made a commit. Then you realized… it was a mistake. Maybe it broke the build. Maybe it deleted something important. Or maybe it just wasn’t ready yet. Don’t panic. Git is built for this. You can undo commits safely and cleanly if you know the right commands.

TL;DR: To undo a commit safely, use git revert <commit>. It creates a new commit that reverses the old one. If the commit has not been pushed, you can use git reset to move your branch pointer. Be careful with git reset --hard because it deletes changes permanently.

Now let’s break it down. Step by step. Simple and fun.


First, Understand What “Revert” Means

In Git, “revert” does not delete history. That’s important. Instead, it adds a new commit that undoes the changes from an earlier commit.

Think of it like this:

  • You baked a cake.
  • You added too much salt.
  • You don’t erase history.
  • You add sugar to balance it.

That balanced fix? That’s git revert.

This makes it very safe. Especially if the commit was already pushed to a shared repository.


When Should You Use git revert?

Use git revert when:

  • The commit is already pushed.
  • Other people may have pulled it.
  • You want a safe and traceable undo.
  • You don’t want to rewrite history.

If you are working in a team, this is usually the best option.


Step 1: Find the Commit You Want to Revert

First, look at your commit history:

git log

You will see something like this:

commit a1b2c3d4
Author: You
Message: Add login validation

commit e5f6g7h8
Author: You
Message: Fix typo

Each commit has a unique hash. You only need the first few characters.

Find the commit you want to undo. Copy its ID.


Step 2: Run git revert

Now run:

git revert a1b2c3d4

Replace a1b2c3d4 with your real commit ID.

Git will:

  • Create a new commit.
  • Reverse the changes.
  • Open your editor for a commit message.

You’ll see a default message like:

Revert "Add login validation"

You can keep it or edit it. Then save and exit.

Done. That’s it.


What If There Are Conflicts?

Sometimes Git cannot automatically reverse everything. This happens if later commits changed the same lines.

Git will tell you there is a conflict.

Don’t panic.

Steps to fix it:

  1. Open the conflicted files.
  2. Look for conflict markers.
  3. Fix the code manually.
  4. Add the resolved files:
git add .

Then continue the revert:

git revert --continue

Now the revert commit is complete.


Reverting the Most Recent Commit

If you want to revert the last commit only, use:

git revert HEAD

HEAD means the latest commit on the current branch.

Super simple. Very handy.


Reverting Multiple Commits

What if you need to revert several commits?

You can revert them one by one:

git revert commit1
git revert commit2

Or revert a range:

git revert OLDER_COMMIT^..NEWER_COMMIT

This reverts all commits in between.

Be careful with order. Git processes them one at a time.


Now Let’s Talk About git reset

This is where things get interesting.

git reset does not create a new commit. It moves the branch pointer. It rewrites history.

That can be powerful. But also dangerous.

There are three main types:

  • –soft
  • –mixed
  • –hard

Let’s explain each one.


1. git reset –soft

git reset --soft HEAD~1

This moves your branch back one commit.

But it keeps all changes staged.

It’s like saying: “Undo the commit, but keep my work ready.”

Great if your commit message was wrong. Or you forgot something.


2. git reset –mixed (default)

git reset HEAD~1

This:

  • Moves the branch back.
  • Unstages the changes.
  • Keeps your code in the working directory.

This is the default mode.

Your files remain changed. Just not staged.


3. git reset –hard

git reset --hard HEAD~1

This is the nuclear option.

It:

  • Moves the branch back.
  • Deletes staged changes.
  • Deletes working directory changes.

Your changes are gone. Completely.

No undo button. Unless you use git reflog.

Use this only if you are 100% sure.


When Should You Use git reset?

Use git reset when:

  • The commit is local.
  • You have not pushed it yet.
  • You want to rewrite history.
  • You are cleaning up commits before pushing.

Do not use it lightly on shared branches.


What If You Already Pushed?

If the commit is already on a shared branch like main:

Use git revert.

Do not use git reset unless:

  • You really understand force pushing.
  • Your team agrees.

Force pushing after reset looks like this:

git push --force

This rewrites remote history.

It can break your teammates’ work.

Be careful.


Bonus: How to Undo a Revert

Yes. You can undo a revert.

Because revert creates a normal commit.

Just revert the revert:

git revert <revert-commit-id>

Simple symmetry. Git is elegant like that.


Using git reflog as a Safety Net

If you accidentally used git reset --hard, don’t give up.

Run:

git reflog

This shows where HEAD has been.

You might see something like:

abc1234 HEAD@{1}: commit: Add feature
def5678 HEAD@{2}: commit: Fix bug

To recover:

git reset --hard abc1234

Your commit is back.

Reflog is like a time machine. Use it wisely.


Best Practices

Here are smart habits to follow:

  • Use git revert for shared branches.
  • Use git reset only for local commits.
  • Avoid –hard unless truly necessary.
  • Check git log before running destructive commands.
  • Create backups with temporary branches.

Example backup:

git branch backup-branch

Now you can experiment safely.


Quick Command Cheat Sheet

  • Revert specific commit:
    git revert <commit>
  • Revert latest commit:
    git revert HEAD
  • Undo last commit but keep changes staged:
    git reset --soft HEAD~1
  • Undo last commit and unstage changes:
    git reset HEAD~1
  • Undo last commit and delete changes:
    git reset --hard HEAD~1
  • Recover lost commits:
    git reflog

Final Thoughts

Making mistakes in Git is normal. Everyone does it. Even senior developers. The difference is they know how to fix them calmly.

If the commit is public, use git revert. It’s safe and clean.

If it’s private and local, git reset gives you flexibility.

And always remember: Git almost never truly deletes anything. There is usually a way back.

So go ahead. Commit boldly. Revert confidently. And code fearlessly.

Previous

How To Revert A Commit In Git: Step-By-Step Commands And Best Practices

Related posts

Leave a Reply

Required fields are marked *