Identify the hash of the commit, using git log, then use git revert <commit> to create a new commit that removes these changes. In a way, git revert is the converse of git cherry-pick -- the latter applies the patch to a branch that's missing it, the former removes it from a branch that has it.

Answer from Andrew Aylett on Stack Overflow
๐ŸŒ
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 ย  5 days ago
Discussions

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
Best way to revert the 3 last commit?
git reset HEAD^^^? More on reddit.com
๐ŸŒ r/git
6
4
October 19, 2017
Undoing last commit
You should use git revert in this situation. It will undo the changes made by the faulty commit. Then push the newly created commit to remote. See https://git-scm.com/docs/git-revert for more info. More on reddit.com
๐ŸŒ r/git
12
6
January 7, 2021
How do you revert your code back to your last git commit?
Don't forget that you can always make a copy of the whole git repo folder on your computer so you can restore if you screw something up. Then you can mess around carefree! More on reddit.com
๐ŸŒ r/learnprogramming
21
1
June 4, 2022
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ git โ€บ revert a git repository to a previous commit
Revert a Git repository to a previous commit | Sentry
In Git, the word โ€œrevertโ€ has a special meaning. You can use the git revert command to return your repositoryโ€™s files to a previous state without rewriting the commit history. This is done by creating new commits that do the opposite of existing commits, i.e.
Top answer
1 of 10
111

The safest and probably cleanest way to go is to rebase interactively.

git rebase -i HEAD^^

Or,

git rebase -i baf8d5e7da9e41fcd37d63ae9483ee0b10bfac8e^

From there you can squash commits, which puts one or more commits together into the previous commit. To completely delete a commit from the history, delete the line from the list.

You can revert a commit with git revert but its going to add more commit messages to the history, which may be undesirable. Use the -n parameter to tell Git not to commit the revert right away. You can rebase interactively and squash those on up to a previous commmit to keep things clean.

If the two commits you're working with here affect the same file(s), you may see a merge conflict.

Resetting the repository with git reset --hard should be done with care, as it cannot be undone.

Rewriting history should be done with care.

2 of 10
70

This if from http://nakkaya.com/2009/09/24/git-delete-last-commit/ and it worked for me

Git Delete Last Commit

Once in a while late at night when I ran out of coffee, I commit stuff that I shouldn't have. Then I spend the next 10 - 15 minutes googling how to remove the last commit I made. So after third time I wanted to make a record of it so I can refer to it later.

If you have committed junk but not pushed,

git reset --hard HEAD~1

HEAD~1 is a shorthand for the commit before head. Alternatively you can refer to the SHA-1 of the hash you want to reset to. Note that when using --hard any changes to tracked files in the working tree since the commit before head are lost.

If you don't want to wipe out the work you have done, you can use --soft option that will delete the commit but it will leave all your changed files "Changes to be committed", as git status would put it.

Now if you already pushed and someone pulled which is usually my case, you can't use git reset. You can however do a git revert,

git revert HEAD

This will create a new commit that reverses everything introduced by the accidental commit.

Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @pinglinh โ€บ how-to-revert-to-a-previous-commit-when-youve-already-pushed-your-changes-910fec9af058
How to revert to a previous commit when youโ€™ve already pushed your changes | by linhothy | Medium
September 25, 2017 - I needed to check the ID log of the commit I wanted to revert back to by using the following command: ... From this, I can see that the previous commit had the ID log of 080ebf7. ... 3. I then copied and pasted my code from the folder where I saved it at the beginning and did git status:
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ git-reverting-to-previous-commit-how-to-revert-to-last-commit
Git Reverting to Previous Commit โ€“ How to Revert to Last Commit
October 19, 2022 - To do that, run the command below: ... As you can see above, this command lists all your commits along with their IDs. To go back to the second commit, you run the git reset command followed by the commit ID.
๐ŸŒ
30 Seconds of Code
30secondsofcode.org โ€บ home โ€บ git โ€บ branch โ€บ rewind to commit
Rewind back to a specific commit in Git - 30 seconds of code
May 26, 2023 - # Syntax: git reset [--hard] HEAD~<n> git reset HEAD~5 # Rewinds back 5 commits but keeps changes in the working directory git reset --hard HEAD~3 # Rewinds back 3 commits and deletes changes ยท The --hard flag is considered a destructive action, ...
๐ŸŒ
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
The purpose of the git revert command is to remove all the changes a single commit made to your source code repository. For example, if a past commit added a file named index.html to the repo, a git revert on that commit will remove the index.html ...
๐ŸŒ
GitHub
docs.github.com โ€บ en โ€บ desktop โ€บ managing-commits โ€บ reverting-a-commit-in-github-desktop
Reverting a commit in GitHub Desktop - GitHub Docs
You can use GitHub Desktop to revert a specific commit to remove its changes from your branch. ... When you revert to a previous commit, the revert is also a commit. The original commit also remains in the repository's history. ... When you revert multiple commits, it's best to revert in order from newest to oldest...
๐ŸŒ
Medium
medium.com โ€บ swlh โ€บ using-git-how-to-go-back-to-a-previous-commit-8579ccc8180f
Using Git โ€” how to go back to a previous commit | by Tolani Benson | The Startup | Medium
June 3, 2020 - Whichever option you use, take a note of the ID of the commit you want to revert to. Use git checkout & the ID (in the same way you would checkout a branch) to go back:
๐ŸŒ
Warp
warp.dev โ€บ terminus by warp โ€บ git โ€บ undoing git commits
How To Undo Your Last Git Commit(s) | Warp
November 30, 2023 - Another approach that could also be used to โ€œundoโ€ commits is git revert. git revert works to undo a change by creating a new commit. This makes it a safe way for undoing changes in a public repository, as it does not change the history.
๐ŸŒ
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
When you commit to your local repository with git commit, Git records your changes. Because you did not push to a remote repository yet, your changes are not public or shared with others. At this point, you can undo your changes. You can revert a commit while retaining the commit history.
๐ŸŒ
JoeHx Blog
joehxblog.com โ€บ revert-an-older-commit-without-discarding-any-later-commits
Revert An Older Commit Without Discarding Any Later Commits โ€“ JoeHx Blog
February 26, 2019 - The commit hash is that last bit of numbers and letters in the GitHub commit url. So the command I typed was: git revert 461914ecb7a057013c0a486b0f445c44bef4bdb9
๐ŸŒ
Linode
linode.com โ€บ docs โ€บ guides โ€บ how-to-undo-git-commit
Undo a Git Commit: A Step-by-Step Guide | Linode Docs
July 8, 2022 - A revert does not throw away or overwrite any earlier commits. While git reset removes all records of the undesirable commit, git revert generates an entirely new commit to accomplish the same undo operation.
๐ŸŒ
Atlassian
atlassian.com โ€บ git โ€บ tutorials โ€บ undoing changes โ€บ git revert
How to Revert a Commit in Git? | Atlassian Git Tutorial
Second, git revert is able to target an individual commit at an arbitrary point in the history, whereas git reset can only work backward from the current commit. For example, if you wanted to undo an old commit with git reset, you would have to remove all of the commits that occurred after the target commit, remove it, then re-commit all of the subsequent commits.
๐ŸŒ
TechTarget
techtarget.com โ€บ searchitoperations โ€บ answer โ€บ How-to-roll-back-Git-code-to-a-previous-commit
How to roll back Git code to a previous commit | TechTarget
When developers need a git rollback to a previous commit, there are two command options that can help: git reset and git revert.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ git โ€บ how-to-back-commit-in-git
How to Back Commit in Git? - GeeksforGeeks
July 23, 2025 - Luckily, Git offers a few ways to undo commits. ... 'git revert' is the generally preferred approach as it creates a new commit that essentially cancels out the changes introduced by the commit you want to undo.