Fundamentals 11 min read

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

This article explains why and how to remove unwanted Git commit history by using git revert to create undo commits, git reset to move the HEAD pointer and discard changes, and finally force‑pushing the cleaned local branch to overwrite the remote repository, with step‑by‑step code examples and safety tips.

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

Why Delete Commit History

A product request led to an accidental commit that should be removed immediately to avoid exposing sensitive code in the remote repository.

Using git revert to Undo Commits

Purpose

git revert creates a new commit that reverses the changes of a previous commit while keeping the original history intact.

Syntax

Revert a single commit:

git revert <commit-hash>

Revert multiple commits:

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

Revert the most recent commit:

git revert HEAD

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

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

Practical Steps

1. Use git log (or a visual tool) to find the hash of the unwanted commit, e.g., b1b56b50a0859556623283946972e495d4a42fc1 . 2. Run git revert b1b56b50a0859556623283946972e495d4a42fc1 . 3. Git opens the default editor (vim). Press i to edit the commit message, type a description such as "Delete erroneous commit", press Esc , then type :wq to save and exit. 4. Push the new revert commit with git push . After the revert, the original changes disappear locally, but the remote history still contains the original commit. Using git reset to Delete Commits Purpose git reset moves the HEAD pointer and can optionally modify the index and working directory, allowing you to discard commits entirely. Syntax Soft reset (keep changes staged): git reset --soft HEAD~1 Mixed reset (keep changes unstaged): git reset HEAD~1 Hard reset (discard all changes): git reset --hard HEAD~1 Reset to a specific commit: git reset --hard <commit-hash> Practical Steps If two unwanted commits exist, move the HEAD back two steps: git reset HEAD~2 This restores the local code to the state before those commits. To remove the same commits from the remote repository, force‑push the cleaned branch: git push --force Force‑pushing overwrites the remote history; it is a dangerous operation and should only be used when you are certain the local and remote branches are synchronized. 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 previous changes, preserving the commit log.

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.