Once you push to the repo, you really don't want to go about changing history. However, if you are absolutely sure that nobody has pulled/fetched from the repo since your offending commit, you have 2 options.

If you want to remove the "bad" commit altogether (and every commit that came after that), do a git reset --hard ABC (assuming ABC is the hash of the "bad" commit's elder sibling — the one you want to see as the new head commit of that branch). Then do a git push --force (or git push -f).

If you just want to edit that commit, and preserve the commits that came after it, do a git rebase -i ABC~. This will launch your editor, showing the list of your commits, starting with the offending one. Change the flag from "pick" to "e", save the file and close the editor. Then make the necessary changes to the files, and do a git commit -a --amend, then do git rebase --continue. Follow it all up with a git push -f.

I want to repeat, these options are only available to you if nobody has done a pull or fetch that contains your offending commit. If they have, doing these steps will just make matters worse.

Answer from David Deutsch on Stack Overflow
Top answer
1 of 5
296

Once you push to the repo, you really don't want to go about changing history. However, if you are absolutely sure that nobody has pulled/fetched from the repo since your offending commit, you have 2 options.

If you want to remove the "bad" commit altogether (and every commit that came after that), do a git reset --hard ABC (assuming ABC is the hash of the "bad" commit's elder sibling — the one you want to see as the new head commit of that branch). Then do a git push --force (or git push -f).

If you just want to edit that commit, and preserve the commits that came after it, do a git rebase -i ABC~. This will launch your editor, showing the list of your commits, starting with the offending one. Change the flag from "pick" to "e", save the file and close the editor. Then make the necessary changes to the files, and do a git commit -a --amend, then do git rebase --continue. Follow it all up with a git push -f.

I want to repeat, these options are only available to you if nobody has done a pull or fetch that contains your offending commit. If they have, doing these steps will just make matters worse.

2 of 5
133

If it's only on your local PC (or noone checked out your changes):

  1. Use:

    git log
    

to find the commit you want to remove. Copy hash (the long sqeuence like: e8348ebe553102018c...).

  1. Use:

    git rebase -i [hash]~
    

    for example:

    git rebase -i e8348~
    

Just remove the commit you don't need and save the file.

Interactive git rebase can let you also fix the broken commit - there is no need to remove it.

If you pushed changes to the server or someone already got your changes - never change history - it'd cause serious problems for your team.

🌐
Git Tower
git-tower.com › learn › git faq › how to undo, revert, or delete a git commit
How to Undo, Revert, or Delete a Git Commit | Learn Version Control with Git
Change pick to drop next to the commit you want to remove, then save and close: Because this rewrites history, a force-push is required afterward if the commits are already on a remote. Only use interactive rebase on private branches that haven't been shared with others. In case you are using the Tower Git client, interactive rebase is accessible directly from the right-click menu, and if you make a mistake, you can undo it with CMD+Z:
Published   1 day ago
Top answer
1 of 5
80

First off, git revert is the wrong command here. That creates a new commit that reverts an older one. That's not what you're asking for. Secondly, it looks like you want to revert HEAD instead of HEAD^.

If you haven't pushed this anywhere, you can use git reset --hard HEAD^ to throw away the latest commit (this also throws away any uncommitted changes, so be sure you don't have any you want to save). Assuming you're ok with the sensitive information being present in your copy and nobody else's, you're done. You can continue to work and a subsequent git push won't push your bad commit.

If that's not a safe assumption (though if not I'd love to hear why), then you need to expire your reflogs and force a garbage collection that collects all outstanding objects right now. You can do that with

git reflog expire --expire=now --expire-unreachable=now --all
git gc --prune=now

though this should only be done if you really absolutely need to do it.


If you have pushed your commit, then you're pretty much out of luck. You can do a force-push to revert it remotely (though only if the remote side allows that), but you can't delete the commit itself from the remote side's database, so anyone who has access to that repository can find it if they know what to look for.

2 of 5
53

If you don't care about the commit, just do:

git reset --hard HEAD~

to blow away the commit.

If you want the changes to be in working directory, do:

git reset HEAD~

Depending on what you have done with git revert, you might have to change the above commands. Revert creates a new commit that reverts the commit you wanted to revert. So there will be two commits. You might have to do HEAD~2 to remove them both.

Note that, usually, revert is the safer way to, well, revert changes. But here, since you want to remove sensitive data, reset is the best approach.

🌐
Reddit
reddit.com › r/git › how to completely remove a commit from git history
r/git on Reddit: How to completely remove a commit from git history
September 26, 2025 -

Hello, I have an unusual git repo which I'm using to create backups of a project with quite a few non-source code files, which have changed more than I expected. I'm actually thinking git might not have been the best tool for the job here, but I'm familiar with it and will probably continue to use it. This is just a personal project, and I'm the only contributor.

What I'm looking for is a way to completely erase a git commit, preferably give the git commit hash. The reason for this is because I have several consecutive commits which change a variety of large files, but I really don't care about the commits in between those which I think would be sufficient to keep. I was thinking there should be a way to remove the unneeded intermediate commits with prune, but am not sure what the best approach here is - thanks!

🌐
Reddit
reddit.com › r/git › is it possible to "remove" changes done by old commit?
r/git on Reddit: Is it possible to "remove" changes done by old commit?
August 17, 2022 -

This is a theoretical question: If I work on a project where I made changes that seem to be OK and not causing issues, but then, let's say after many other commits and merges with the entire team we realize that the commit I did actually caused some damage that needs to be undone..

for example, I refactored some variable name across the entire project in all occurences and only later realized some were supposed to remain unchanged.

Is it possible to "remove" this commit from the project while keeping all the other merges? Assuming they do not conflict or have anything to do with this change

🌐
GitLab
docs.gitlab.com › topics › git › undo
Revert and undo changes | GitLab Docs
... If a file was changed in a commit, and you want to change it back to how it was in the previous commit, but keep the commit history, you can use git revert. The command creates a new commit that reverses all actions taken in the original commit. For example, to remove a file’s changes ...
🌐
Atlassian
atlassian.com › git › tutorials › undoing changes › git revert
How to Revert a Commit in Git? | Atlassian Git Tutorial
The git revert command is used for undoing changes to a repository's commit history. Other 'undo' commands like, git checkout and git reset, move the HEAD and branch ref pointers to a specified commit.
Find elsewhere
🌐
Redirect
sethrobertson.github.io › GitFixUm
On undoing, fixing, or removing commits in git
If you see this message, your browser is broken. Please click here
🌐
Ben Holmes
bholmes.dev › blog › how-to-revert-your-git-commits-without-panicking
How to revert your git commits without panicking - Ben Holmes
After reverting, it will appear your branch is behind the remote, so you cannot push directly anymore. In this case, you will have to run git push --force-with-lease. This flag will force your history onto the remote by removing those dropped ...
🌐
Warp
warp.dev › terminus by warp › git › undoing git commits
How To Undo Your Last Git Commit(s) | Warp
November 30, 2023 - Explore ways to undo a commit, including git reset, git checkout, and git revert with git while preserving commit history.
🌐
Gitprogress
gitprogress.com › blog › git-remove-commit-from-history
How to Remove a Commit from Git History Safely — Git Progress
March 10, 2026 - # 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.
🌐
GitKraken
gitkraken.com › home › learn › problems & solutions › how to revert a git commit
Git Revert Commit | Solutions to Git Problems
February 5, 2024 - Learn how to use Git revert to undo changes introduced in a specified commit or group of commits. See examples of Git revert commit in the terminal, GitKraken Client, & GitLens.
🌐
GeeksforGeeks
geeksforgeeks.org › git › overturning-changes-in-git
Reverting a Git Commit - GeeksforGeeks
January 20, 2026 - ... git revert creates a new commit that safely undoes a previous commit without changing branch history. ... -e or --edit: This option lets you edit the commit message prior to committing the revert.
🌐
DEV Community
dev.to › edriso › how-to-undo-a-git-commit-without-losing-history-3ifp
How to Undo a Git Commit Without Losing History - DEV Community
April 23, 2026 - Your mistake is undone and your history is clean. Rule of thumb: If the commit is already pushed, use git revert. If it's only local, git reset is fine. # See your commits git log --oneline # Revert the one you don't want git revert <commit-hash> ...
🌐
LabEx
labex.io › tutorials › git-how-to-undo-and-remove-a-specific-git-commit-from-current-branch-392832
How to Undo and Remove a Specific Git Commit from Current Branch | LabEx
To remove the commit, change the word pick to drop (or d) for the line containing "fix: Add a temporary file that should be removed". ... In vim, press i to enter insert mode, make your changes, then press Esc to exit insert mode.
🌐
GoLinuxCloud
golinuxcloud.com › home › devops › git › how to remove a commit in git (undo, delete or revert) with examples
How to Remove a Commit in Git (Undo, Delete or Revert) with Examples | GoLinuxCloud
March 16, 2026 - If you want to completely remove the commit from history, you must reset the branch and force push. ... Using git revert is preferred when working with shared repositories.
🌐
Atlassian
atlassian.com › git › tutorials › undoing changes
Undoing Changes in Git | Atlassian Git Tutorial
December 15, 2025 - A revert is safer than a reset because it will not remove any commits from a shared history. A revert will retain the commits you want to undo and create a new commit that inverts the undesired commit.
🌐
Towards Data Science
towardsdatascience.com › home › latest › git undo : how to rewrite git history with confidence
Git UNDO : How to Rewrite Git History with Confidence | Towards Data Science
April 22, 2026 - If I override those changes by using git reset, as we’ve done so far, we will have different histories and all hell might break loose. You can rewrite your own copy of the repo as much as you like until you push it. Once you push the change, you need to be very certain no one else has fetched those changes if you are going to rewrite history. Alternatively, you can use another tool called git revert. This command takes the commit you’re providing it with, compute the Diff from its parent commit, just like git cherry-pick, but this time it computes the reverse changes.
🌐
Sentry
sentry.io › sentry answers › git › revert a git repository to a previous commit
Revert a Git repository to a previous commit | Sentry
To do this, you can use git reset --hard, specifying the commit to return to: ... This will return the repository’s files to their previous state and remove the most recent commit from the current branch’s history.
🌐
TheServerSide
theserverside.com › tutorial › How-to-git-revert-a-commit-A-simple-undo-changes-example
How to revert a Git commit: A simple example | TheServerSide
Let's walk through an example of ... reset and git revert commands. The purpose of the git revert command is to remove all the changes a single commit made to your source code repository.