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
Undo the last commit with git reset, revert an older commit with git revert, or remove a commit from history with interactive rebase. All scenarios, step by step.
Published   6 days ago
Discussions

git - How to revert last commit and remove it from history? - Stack Overflow
I did a commit and reverted with git revert HEAD^ just git log ➜ git:(master) git log commit 45a0b1371e4705c4f875141232d7a97351f0ed8b Author: Daniel Palacio Date: Tue... More on stackoverflow.com
🌐 stackoverflow.com
Is it possible to "remove" changes done by old commit?
Use git revert . It doesn't "remove" the old commit; it adds a new commit that simply reverses the old commit's change. More on reddit.com
🌐 r/git
8
6
August 17, 2022
git checkout - How do I revert a Git repository to a previous commit? - Stack Overflow
Idea: You basically want to replace ... commit and then create a commit out of it. Ignored files should best be not changed. ... Emtpy the working tree *. ... Bring the working tree in the state we want **. ... Create the revert commit. git add --all && git commit -m "revert to 0d1d7fc3" It does not delete anything (pushed or upushed) from the history. It produces one clean commit which represents the state we want to revert back to. * by removing untracked ... More on stackoverflow.com
🌐 stackoverflow.com
How to completely remove a commit from git history
Do an interactive rebase and drop the commit in question. More on reddit.com
🌐 r/git
18
6
September 26, 2025
🌐
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!

🌐
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.
🌐
GitProtect.io
gitprotect.io › strona główna › git undo: 13 ways to undo mistakes in git
Git Undo: 13 Ways to Undo Mistakes in Git - Blog | GitProtect.io
November 24, 2025 - Check your status: Before running ... what will be affected. – Use Undo commands wisely: Commands like “` git reset and “` git revert ......
🌐
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 ...
Find elsewhere
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 › 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

🌐
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.
🌐
CloudBees
cloudbees.com › blog › git-undo-commit
How to Undo Changes in Git with Git Undo Commit
November 25, 2020 - If you must undo changes in a shared branch of a shared repo, the best tool to use is git revert <commit id>. It reverts the changes done by the commit you specified, and then it creates a new commit for it.
🌐
Aviator
aviator.co › home › blog › how to git undo commit: methods and best practices
How to Git Undo Commit: Methods and Best Practices - Aviator Blog
February 10, 2025 - This creates a new commit that reverses the changes of a specific commit, leaving the commit history intact. Imagine you’re working on a platform for customer subscriptions and pushing a commit that accidentally removes the logic for automatic renewals. Other developers may have already pulled the changes, so you can’t use git reset. Instead, you use git revert to undo the changes safely.
🌐
DEV Community
dev.to › github › how-to-undo-pushed-commits-with-git-2pe6
How to Undo Pushed Commits with Git - DEV Community
April 6, 2022 - So to have multiple commits reverted, we have to do git revert multiple times ? If so I assume from the top commit. Any insights on that? ... Full stack web developer! PS: The "crown" in my profile picture is a cut-off copper winding from an electric motor :) ... As the article mentioned, you can revert multiple commits by reverting an entire branch or a tag. And no, you don’t need to start from the top. You can pick any commit or group of commits in the repo’s history to revert.
🌐
Better Stack
betterstack.com › community › questions › how-to-remove-git-commit-not-pushed
Remove a Git Commit Which Has Not Been Pushed | Better Stack Community
Hard Reset: git reset --hard HEAD~1 - Completely remove the last commit and discard changes. Soft Reset: git reset --soft HEAD~1 - Remove the last commit but keep changes in the working directory.
🌐
Ben Holmes
bholmes.dev › blog › how-to-revert-your-git-commits-without-panicking
How to revert your git commits without panicking
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 ...
🌐
Xebia
xebia.com › home › blog › how to delete git commit history – a step-by-step guide
How To Remove Git Commit History: Step-by-Step Guide For GitHub Users
1 month ago - Delete the local default branch ... default with git branch -m main, and force update the remote repository with git push --force origin main....
🌐
Atlassian
atlassian.com › git › tutorials › resetting checking out and reverting
Resetting, Checking Out & Reverting | Atlassian Git Tutorial
December 16, 2025 - For example, the following command makes foo.py in the working directory match the one from the 2nd-to-last commit: ... Just like the commit-level invocation of git checkout, this can be used to inspect old versions of a project—but the scope is limited to the specified file. If you stage and commit the checked-out file, this has the effect of “reverting” to the old version of that file. Note that this removes all of the subsequent changes to the file, whereas the git revert command undoes only the changes introduced by the specified commit.
Top answer
1 of 16
12602

This depends a lot on what you mean by "revert".

Temporarily switch to a different commit

If you want to temporarily go back to it, fool around, then come back to where you are, all you have to do is check out the desired commit:

# This will detach your HEAD, that is, leave you with no branch checked out:
git checkout 0d1d7fc32

Or if you want to make commits while you're there, go ahead and make a new branch while you're at it:

git checkout -b old-state 0d1d7fc32

To go back to where you were, just check out the branch you were on again. (If you've made changes, as always when switching branches, you'll have to deal with them as appropriate. You could reset to throw them away; you could stash, checkout, stash pop to take them with you; you could commit them to a branch there if you want a branch there.)

Hard delete unpublished commits

If, on the other hand, you want to really get rid of everything you've done since then, there are two possibilities. One, if you haven't published any of these commits, simply reset:

# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc32

# Alternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc32
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts, if you've modified things which were
# changed since the commit you reset to.

If you mess up, you've already thrown away your local changes, but you can at least get back to where you were before by resetting again.

Undo published commits with new commits

On the other hand, if you've published the work, you probably don't want to reset the branch, since that's effectively rewriting history. In that case, you could indeed revert the commits. In many enterprise organisations, the concept of "protected" branches will even prevent history from being rewritten on some major branches. In this case, reverting is your only option.

With Git, revert has a very specific meaning: create a commit with the reverse patch to cancel it out. This way you don't rewrite any history.

First figure out what commits to revert. Depending on the technique chosen below, you want to either revert only the merge commits, or only the non-merge commits.

# This lists all merge commits between 0d1d7fc and HEAD:
git log --merges --pretty=format:"%h" 0d1d7fc..HEAD | tr '\n' ' '

# This lists all non merge commits between 0d1d7fc and HEAD:
git log --no-merges --pretty=format:"%h" 0d1d7fc..HEAD | tr '\n' ' '

Note: if you revert multiple commits, the order matters. Start with the most recent commit.

# This will create three separate revert commits, use non merge commits only:
git revert a867b4af 25eee4ca 0766c053

# It also takes ranges. This will revert the last two commits:
git revert HEAD~2..HEAD

# Similarly, you can revert a range of commits using commit hashes (non inclusive of first hash):
git revert 0d1d7fc..a867b4a

# Reverting a merge commit. You can also use a range of merge commits here.
git revert -m 1 <merge_commit_sha>

# To get just one, you could use `rebase -i` to squash them afterwards
# Or, you could do it manually (be sure to do this at top level of the repo)
# get your index and work tree into the desired state, without changing HEAD:
git checkout 0d1d7fc32 .

# Then commit. Be sure and write a good message describing what you just did
git commit

The git-revert manpage actually covers a lot of this in its description. Another useful link is this git-scm.com section discussing git-revert.

If you decide you didn't want to revert after all, you can revert the revert (as described here) or reset back to before the revert (see the previous section).

You may also find this answer helpful in this case:
How can I move HEAD back to a previous location? (Detached head) & Undo commits

2 of 16
3981

Lots of complicated and dangerous answers here, but it's actually easy:

git revert --no-commit 0d1d7fc3..HEAD
git commit
git push

This will revert everything from the HEAD back to the commit hash (excluded), meaning it will recreate that commit state in the working tree as if every commit after 0d1d7fc3 had been walked back. You can then commit the current tree, and it will create a brand new commit essentially equivalent to the commit you "reverted" to.

(The --no-commit flag lets git revert all the commits at once- otherwise you'll be prompted for a message for each commit in the range, littering your history with unnecessary new commits.)

This is a safe and easy way to rollback to a previous state. No history is destroyed, so it can be used for commits that have already been made public.


Note on merge commits:
If one of the commits between 0766c053..HEAD (inclusive) is a merge then there will be an error popping up (to do with no -m specified). The following link may help those encountering that: Why does git revert complain about a missing -m option? (thanks @timhc22 for pointing out)

🌐
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.