How to Remove a Commit from Git: A Practical Guide

Sometimes a commit ends up in your Git history that you wish wasn’t there. Maybe it includes a bug, a secret key, or just a mistake. Git offers several powerful commands to help you remove a commit—whether it’s your last commit or one buried deeper in your history.

In this guide, you’ll learn multiple ways to remove a commit from Git, tailored to different situations.


🧠 First, Ask Yourself:

Before choosing how to remove a commit, determine:

  • 📍 Is the commit local or already pushed to GitHub?
  • 📆 Is it the most recent commit, or further back in history?
  • 🧑‍🤝‍🧑 Are others working on this branch?

Your answers will determine whether to rewrite history or revert the commit safely.


✅ Option 1: Remove the Most Recent Commit (Local)

1. Keep the changes (unstage them):

git reset --soft HEAD~1

This removes the last commit but keeps your changes staged.

2. Keep the changes (unstaged):

git reset --mixed HEAD~1

Removes the last commit and unstages the changes, but doesn’t delete them.

3. Discard the changes entirely:

git reset --hard HEAD~1

This deletes the last commit and all associated changes. ⚠️ Use with caution.


🔁 Option 2: Remove a Specific Commit from History (Local)

If you want to remove a commit that’s not the latest, use an interactive rebase:

git rebase -i HEAD~n

Replace n with the number of recent commits to view/edit. For example:

git rebase -i HEAD~5

Then:

  1. In the editor, find the commit you want to remove.
  2. Change the word pick to drop.
  3. Save and close the editor.

Finish the rebase, and the commit will be removed from your local history.


🚀 Option 3: Remove a Commit That Has Been Pushed to GitHub

A. Revert the Commit (Safe for Shared Branches)

git revert <commit-hash>

This creates a new commit that undoes the changes of the one you’re removing—preserving history.

Best for:

  • Shared branches
  • Public repositories
  • Collaborative environments

B. Force Push After Reset or Rebase (Risky)

If you’re sure it’s safe to rewrite history:

git reset --hard <commit-hash>
git push --force

Or after an interactive rebase:

git rebase -i HEAD~n
git push --force

⚠️ Warning: Force-pushing can break other collaborators’ workflows. Only use it on private or solo branches.


📝 Summary

TaskCommand ExampleSafe for Shared Branches?
Remove last commit (keep changes)git reset --soft HEAD~1Yes (if not pushed)
Remove last commit (discard changes)git reset --hard HEAD~1No
Remove specific commit (local)git rebase -i HEAD~n → dropNo
Revert pushed commit safelygit revert <commit-hash>✅ Yes
Rewrite pushed history (risky)git reset/rebase + git push --force❌ No (unless you’re solo)

🧼 Pro Tips

  • 🧯 Backup your branch before risky operations:
git branch backup-before-reset
  • 🕵️ Find commit hashes with:
git log --oneline
  • 🔒 Never remove commits with secrets — rotate them immediately.

🚀 Final Thoughts

Removing a commit from Git can be simple or complex depending on your context. Whether you’re fixing a small mistake or cleaning up your history, Git gives you precise tools to manage your codebase professionally. When in doubt, revert safely or ask your team before rewriting shared history.

Sharing Is Caring:

Leave a Comment