Fundamentals 11 min read

How to Delete a Git Commit from History Using git revert, git reset and Force Push

This article explains why you might need to remove a mistaken Git commit, demonstrates how to use git revert and git reset with various options, shows step‑by‑step commands—including force‑pushing to remote—to completely erase the unwanted commit while preserving or discarding history as required.

Top Architect
Top Architect
Top Architect
How to Delete a Git Commit from History Using git revert, git reset and Force Push

In a recent project the author accidentally pushed a faulty commit that was visible to the whole team, prompting an urgent need to delete that commit from the repository history.

Why Delete Commit History

The author needed to remove the erroneous commit immediately to avoid it being discovered by others.

Using git revert to Undo a Commit

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

Syntax

Revert a single commit:

git revert <commit-hash>

Revert multiple commits:

git revert <commit-hash1> <commit-hash2> ...

Revert the latest commit:

git revert HEAD

Revert a range of commits (excluding the start, including the end):

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

Practical Steps

1. Find the commit hash with git log (e.g., b1b56b50a0859556623283946972e495d4a42fc1 ).

2. Run git revert b1b56b50a0859556623283946972e495d4a42fc1 . Git opens the default editor (vim) to edit the revert commit message; after saving and exiting, push the changes.

Using git reset to Delete a Commit

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

Syntax

Undo the last commit but keep changes staged:

git reset --soft HEAD~1

Undo the last commit and unstage files (changes stay in the working directory):

git reset HEAD~1

Undo the last commit and discard all changes:

git reset --hard HEAD~1

Move HEAD to a specific commit (discarding later commits):

git reset --hard <commit-hash>

Practical Steps

Because the local repository had no other modifications, the author used git reset HEAD~1 and then git reset HEAD~2 to move the HEAD pointer back two commits, restoring the code to the state before the erroneous commits.

The remote repository still contained the bad commits, so a forced push was required:

git push --force

or explicitly:

git push origin <branch-name> --force

After the force push, the remote history no longer shows the unwanted commits.

Summary

git reset moves the branch pointer and can rewrite history without creating a new commit, while git revert creates a new commit that undoes changes, preserving the original history. Using git reset together with git push --force allows you to completely delete an erroneous commit from both local and remote repositories.

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.