Git Advanced9 min readMarch 10, 2026

How to Remove a Commit from Git History Safely

Step-by-step methods to remove, revert, or clean commits from git history — with clear guidance on when each approach is safe to use.

When Should You Remove a Commit?

The most common reasons to remove a commit from history: you accidentally committed a secret (API key, password), committed a large binary file, or made a commit on the wrong branch.

Before you do anything destructive, understand the difference between rewriting history (dangerous if pushed) and reverting (always safe). If the commit has been pushed to a shared branch, revert is almost always the right choice.

Revert a Commit (Safe for Shared Branches)

git revert creates a new commit that undoes the changes of a previous commit. The original commit stays in history — you are adding a correction, not erasing the past. This is always safe, even on shared branches.

# Revert the most recent commit
git revert HEAD

# Revert a specific commit by hash
git revert abc1234

# Revert without auto-committing (inspect changes first)
git revert --no-commit abc1234

Reset to Remove Recent Commits (Local Only)

git reset moves the branch pointer backward, effectively "un-committing" recent changes. Use --soft to keep changes staged, --mixed (default) to keep changes unstaged, or --hard to discard everything.

# Remove the last commit but keep changes staged
git reset --soft HEAD~1

# Remove the last 3 commits, keep files but unstage
git reset HEAD~3

# Remove the last commit and discard all changes
git reset --hard HEAD~1

Warning: --hard is destructive and cannot be easily undone. Only use reset on commits that have not been pushed. If you have already pushed, use git revert instead.

Interactive Rebase to Edit History

Interactive rebase lets you reorder, squash, edit, or drop individual commits. It is the most flexible tool for cleaning up history before pushing.

# Rebase the last 5 commits interactively
git rebase -i HEAD~5

# In the editor, change "pick" to:
# "drop" — remove the commit entirely
# "squash" — merge into the previous commit
# "edit" — pause to amend the commit
# "reword" — change the commit message

Remove Sensitive Data from All History

If you accidentally committed a secret (API key, password, credential file), you need to remove it from every commit in history — not just the latest one. The git filter-repo tool (successor to filter-branch) is the recommended approach.

# Install git-filter-repo
pip install git-filter-repo

# Remove a file from all history
git filter-repo --path secrets.env --invert-paths

# After rewriting, force push (coordinate with your team)
git push --force-with-lease

After removing a secret from history, you must also rotate the credential immediately. Anyone who cloned the repo before the rewrite still has the old history.

Stop running git log manually

Git Progress aggregates your commit history across GitHub, GitLab, and Bitbucket into beautiful dashboards. Free for individual developers.

Get Started Free