Fundamentals 12 min read

How to Delete Commit History with git revert and git reset

This article explains why you might need to remove a commit from Git history, demonstrates how to use git revert to create a new commit that undoes changes while preserving history, and shows how git reset combined with a forced push can completely erase unwanted commits from both local and remote repositories.

Top Architect
Top Architect
Top Architect
How to Delete Commit History with git revert and git reset

In a recent scenario the author needed to delete an accidental commit that was already pushed to a remote repository, prompting an exploration of two Git commands: git revert and git reset .

Why Delete Commit History

The author realized that the mistaken commit was visible to everyone in the remote repository, which could cause serious issues if discovered, so an immediate removal was required.

Using git revert to Undo a Commit

Purpose: git revert creates a new commit that reverses the changes of a specified commit, keeping the original history intact.

Syntax examples:

git revert <commit-hash>
git revert <commit-hash1> <commit-hash2> ...
git revert HEAD
git revert <commit-hash1>^..<commit-hash2>

The command opens the default editor (often Vim) for the user to edit the commit message before saving and exiting.

Practical git revert Example

After locating the unwanted commit hash (e.g., b1b56b50a0859556623283946972e495d4a42fc1 ) with git log , the author ran:

git revert b1b56b50a0859556623283946972e495d4a42fc1

After editing the commit message in Vim and pushing, the unwanted changes disappeared from the code, but the revert commit remained in the remote history.

Using git reset to Remove Commits

Purpose: git reset moves the HEAD pointer and can modify the index and working directory, allowing you to erase commits entirely.

Common forms:

git reset --soft HEAD~1

Moves HEAD back one commit while keeping changes staged.

git reset HEAD~1

Moves HEAD back one commit and unstages changes.

git reset --hard HEAD~1

Moves HEAD back one commit and discards all changes.

git reset --hard <commit-hash>

Resets to a specific commit, discarding later work.

Practical git reset Example

To remove two erroneous commits, the author executed:

git reset HEAD~2

After resetting locally, the code returned to the state before those commits, but the remote still contained them.

Force-Pushing to Overwrite Remote History

To delete the commits from the remote repository, the author used a forced push:

git push --force

or explicitly:

git push origin <branch-name> --force

This overwrites the remote history with the local state, effectively erasing the unwanted commits. The author warns that using --force is dangerous and should only be done when you are certain the local and remote histories are compatible.

Summary

git revert creates a new commit that undoes changes while preserving history, whereas git reset moves the branch pointer and can completely remove commits, especially when combined with git push --force to rewrite remote history.

gitVersion Controlcommit historyforce pushgit resetgit revert
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.