Connect with us

Blog

Revert Local Git Commit: Step-by-Step Guide

Published

on

Git Commit

Reverting a local commit in Git is a common operation for developers working with version control. Whether you’ve committed something by mistake or just want to undo recent changes, Git offers several flexible tools to help. This guide will walk you through the different ways to revert local commits, based on the situation you’re facing.

Understanding the Difference: Revert vs Reset

Before diving into the steps, it’s important to understand the difference between git revert and git reset.

  • git revert creates a new commit that undoes the changes of a previous commit. This is used primarily for shared/public branches.
  • git reset undoes commits and optionally discards changes entirely. This is commonly used for local history editing.

For local commits (especially before pushing), git reset is typically more useful.

Use Case: Undo the Last Local Commit

If you’ve just made a commit and realize you need to undo it, use this command:

bash

CopyEdit

git reset –soft HEAD~1

What It Does:

  • This removes the last commit from the history.
  • It keeps your changes staged, so you can recommit if needed.

If you want to keep the changes but unstage them, use:

bash

CopyEdit

git reset –mixed HEAD~1

And if you want to remove the commit and delete the changes, use:

bash

CopyEdit

git reset –hard HEAD~1

Warning: –hard will delete all uncommitted changes—use with caution.

Use Case: Undo a Specific Commit (Not the Last One)

Sometimes, you need to remove or undo a commit that’s not the most recent. In that case, first find the commit hash:

bash

CopyEdit

git log

Copy the hash (e.g., abc1234), then use:

bash

CopyEdit

git rebase -i abc1234^

This opens an interactive rebase screen where you can choose to:

  • drop a commit (remove it)
  • edit a commit (change message or content)
  • reword a commit message

Alternatively, to undo a specific commit but keep it in the log, use:

bash

CopyEdit

git revert <commit-hash>

This will create a new commit that reverses the effects of the specified commit.

Use Case: Undo Multiple Local Commits

Want to undo several commits in one go? Easy:

bash

CopyEdit

git reset –soft HEAD~N

Replace N with the number of commits to undo. Example: to undo the last 3 commits:

bash

CopyEdit

git reset –soft HEAD~3

If you want to delete all associated changes too:

bash

CopyEdit

git reset –hard HEAD~3

Again—be very cautious with –hard.

Use Case: Undo a Commit That Was Already Pushed

If the commit has already been pushed to a remote repository, you need to be extra careful.

Use git revert:

bash

CopyEdit

git revert <commit-hash>

This creates a new commit that reverses the previous one, preserving history and avoiding breaking others’ work.

If you must force a reset on a remote (not recommended for shared branches):

bash

CopyEdit

git reset –hard <commit-hash>

git push origin HEAD –force

Important: Forcing a push will overwrite the history in the remote repository and can disrupt other collaborators.

Interactive Rebase: A Cleaner Way to Edit Commit History

To clean up several recent commits, use:

bash

CopyEdit

git rebase -i HEAD~N

This opens an editor listing the last N commits. You can:

  • pick: keep the commit as is
  • reword: change the commit message
  • edit: modify the commit contents
  • squash: combine multiple commits into one
  • drop: delete a commit

After editing, save and close the file, then follow the prompts to complete the rebase.

Use Case: Undoing Commit Message Only

If all you want is to change a commit message, and it hasn’t been pushed yet:

bash

CopyEdit

git commit –amend

This lets you edit the message of the latest commit without changing its contents.

If you need to change the message of an older commit:

bash

CopyEdit

git rebase -i HEAD~N

Use reword in the editor for the commit you want to change.

Use Case: Undo a Merge Commit

To undo a local merge commit before pushing:

bash

CopyEdit

git reset –hard ORIG_HEAD

ORIG_HEAD is a reference to the state before the merge, and this command resets your branch to that point.

Alternatively, if you’ve already pushed:

bash

CopyEdit

git revert -m 1 <merge-commit-hash>

Use -m 1 to specify the parent branch you want to keep.

Recovering Lost Commits After a Reset

If you’ve reset something by accident and lost work, Git might still have it. Try:

bash

CopyEdit

git reflog

This shows a log of where your HEAD has been. You can often recover lost commits from here using:

bash

CopyEdit

git reset –hard <commit-hash>

Best Practices Before Reverting

Git Commit
  • Check Status: Use git status before making changes to know where you are.
  • Backup: Before any destructive reset (–hard), create a backup branch:

bash

CopyEdit

git branch backup-before-reset

  • Avoid Force Push on Shared Branches: Coordinate with your team or use git revert instead.

Quick Command Reference

TaskCommand
Undo last commit, keep changes stagedgit reset –soft HEAD~1
Undo last commit, unstage changesgit reset –mixed HEAD~1
Undo last commit, delete changesgit reset –hard HEAD~1
Revert a pushed commit safelygit revert <hash>
Undo specific commitgit rebase -i <hash>^
Change last commit messagegit commit –amend
Undo merge commitgit reset –hard ORIG_HEAD
Recover lost commitsgit reflog

Final Thoughts

Mastering how to revert local commits in Git is an essential skill for every developer. Whether you’re fixing a simple mistake, rewriting history for clarity, or undoing complex merges, Git provides powerful tools. The key is knowing when to use reset vs. revert, understanding the implications of each command, and always using caution—especially when dealing with public branches or irreversible resets.

When in doubt: make a backup, ask your team, and proceed carefully. Git gives you full control over your code history, but with great power comes great responsibility.

Continue Reading
Click to comment

Leave a Reply

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

Trending