How to Delete Git Commit History Using git revert and git reset
This article explains how to remove unwanted Git commit history by using git revert to create a new reversing commit and git reset (soft, mixed, or hard) to move the HEAD pointer, followed by a forced push to overwrite the remote repository, while warning about the risks involved.
When an accidental commit is pushed to a shared repository, you may need to delete it without leaving traces. This guide shows two Git commands for that purpose: git revert and git reset .
Using git revert to undo a commit
git revert <commit-hash> creates a new commit that reverses the changes of the specified commit, preserving history. You can revert a single commit, multiple commits, the latest commit ( git revert HEAD ), or a range ( git revert <hash1>^..<hash2> ). After running the command, Git opens an editor for the revert commit message.
git revert b1b56b50a0859556623283946972e495d4a42fc1After committing the revert, push the changes with git push . The remote history still shows the revert commit.
Using git reset to rewrite history
git reset moves the HEAD pointer and can modify the index and working tree. Common forms are:
git reset --soft HEAD~1 – undo the last commit but keep changes staged.
git reset HEAD~1 – undo the last commit and unstage changes.
git reset --hard HEAD~1 – discard the last commit and all changes.
git reset --hard <commit-hash> – move HEAD to a specific commit, discarding later work.
After resetting locally, the remote still contains the unwanted commits. To remove them from the remote, force‑push the rewritten history:
git push --forceor equivalently:
git push origin <branch-name> --forceImportant: Using --force overwrites the remote history and can be dangerous; ensure the local branch is correct and coordinate with teammates before doing so.
Conclusion
git revert safely undoes changes while keeping a record, whereas git reset (combined with a forced push) can completely erase unwanted commits from both local and remote histories.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.