Fundamentals 10 min read

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

This article explains when and how to use git revert to undo commits while preserving history, demonstrates git reset for removing commits entirely, and shows how to force‑push the cleaned state to a remote repository, including safety warnings and practical command examples.

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

When an accidental commit is pushed to a shared repository, you may need to remove it without leaving traces. The article first introduces git revert <commit-hash> , which creates a new commit that undoes the changes of the specified commit while keeping the original history intact.

It then details the syntax for reverting a single commit, multiple commits, the latest commit, and a range of commits, and shows how Git opens an editor for the revert commit message.

Next, the tutorial moves to git reset , a more powerful command that moves the HEAD pointer. It covers three common reset modes:

git reset --soft HEAD~1 – moves HEAD back one commit but keeps all changes staged.

git reset HEAD~1 – moves HEAD back one commit and unstages the changes, leaving them in the working directory.

git reset --hard HEAD~1 – discards the commit and all associated changes, restoring the repository to the previous state.

For cases where multiple erroneous commits need to be removed, the article shows using git reset HEAD~2 to move the HEAD pointer back two commits.

Since the remote repository still contains the unwanted history, the guide advises a forced push: git push --force (or the equivalent git push origin <branch-name> --force ) to overwrite the remote history with the cleaned local state. It emphasizes that this operation is dangerous and should only be performed when you are certain the local and remote histories are compatible.

The article concludes with a comparison: git reset changes the branch pointer and can delete history without creating new commits, whereas git revert preserves history by adding a new commit that reverses the changes.

Throughout, the tutorial includes practical command snippets wrapped in ... tags and screenshots illustrating the before‑and‑after states of the repository.

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.