You can revert individual commits with:

git revert <commit_hash>

This will create a new commit which reverts the changes of the commit you specified. Note that it only reverts that specific commit, and not commits that come after that. If you want to revert a range of commits, you can do it like this:

git revert <oldest_commit_hash>..<latest_commit_hash>

It reverts all the commits after <oldest_commit_hash> up to and including <latest_commit_hash>. Some versions of git also revert the <oldest_commit_hash> itself, so double check if that commit gets reverted or not. You can always drop the latest revert commit (which reverts the oldest commit) with g reset --hard HEAD~.

To find out the hash of the commit(s) you can use git log.

Look at the git-revert man page for more information about the git revert command. Also, look at this answer for more information about reverting commits.

Extra note for re-applying reverted commits:

Usually you revert commits because you discovered the commits that you pushed turn out to have an issue. Then you first want to restore the repo to a stable state, before you continue to fix the issue. So then you would first do the git revert, as described before. Then push those revert commits, so the remote is stable. After that you would want to re-apply the reverted commits locally, so your local repo's files are back to the state before the revert. Then you can fix the issue.

To re-apply the reverted commits, you have to revert the revert commits. So what you would do, is to again execute the git revert command, but then with the range of commits of the revert commits. So your new revert commits will revert the previous revert commits. Then your files are back to the state before the first revert. Then you can fix the issue, create a new commit with the fix, and then push all the new commits.

Answer from gitaarik on Stack Overflow
Top answer
1 of 16
1910

You can revert individual commits with:

git revert <commit_hash>

This will create a new commit which reverts the changes of the commit you specified. Note that it only reverts that specific commit, and not commits that come after that. If you want to revert a range of commits, you can do it like this:

git revert <oldest_commit_hash>..<latest_commit_hash>

It reverts all the commits after <oldest_commit_hash> up to and including <latest_commit_hash>. Some versions of git also revert the <oldest_commit_hash> itself, so double check if that commit gets reverted or not. You can always drop the latest revert commit (which reverts the oldest commit) with g reset --hard HEAD~.

To find out the hash of the commit(s) you can use git log.

Look at the git-revert man page for more information about the git revert command. Also, look at this answer for more information about reverting commits.

Extra note for re-applying reverted commits:

Usually you revert commits because you discovered the commits that you pushed turn out to have an issue. Then you first want to restore the repo to a stable state, before you continue to fix the issue. So then you would first do the git revert, as described before. Then push those revert commits, so the remote is stable. After that you would want to re-apply the reverted commits locally, so your local repo's files are back to the state before the revert. Then you can fix the issue.

To re-apply the reverted commits, you have to revert the revert commits. So what you would do, is to again execute the git revert command, but then with the range of commits of the revert commits. So your new revert commits will revert the previous revert commits. Then your files are back to the state before the first revert. Then you can fix the issue, create a new commit with the fix, and then push all the new commits.

2 of 16
870

A solution that keeps no traces of the "undo".

NOTE: Don't do this if someone already pulled your change (I would use this only on my personal repo.)

Run:

git reset <previous label or sha1>

Note: previous means the commit before the erroneous commit

This will re-checkout all the updates locally (so git status will list all updated files, meaning, the files you changed/added/.. and were committed).

Then you "do your work" and re-commit your changes (Note: This step is optional).

git commit -am "blabla"

At this moment your local tree differs from the remote

git push -f <remote-name> <branch-name>

will force the remote branch to take this push and remove the previous one (specifying remote-name and branch-name is not mandatory but is recommended to avoid updating all branches with update flag).

!! Watch-out some tags may still be pointing removed commit !! how-to-delete-a-remote-tag

🌐
Graphite
graphite.com › guides › git-revert-commit-after-pushing
git revert commit after pushing - Graphite
Simply use git revert on the revert commit hash: ... To revert multiple commits in reverse chronological order (newest first), you can revert them one by one: ... Whether you've made a mistake in your commit or simply want to modify it before ...
Discussions

How to undo pushed commits
Yes, it's called a force push. Reset your local branch to the commit you want. Assuming you have main checked out: git reset --hard COMMIT_ID with COMMIT_ID being any ref you want, either a commit id, origin/main^^, etc. Then force push the branch: git push -f origin main To ensure no one else has pushed work on top of main in the meantime, you should use --force-with-lease. git push --force-with-lease origin main (edit): In your case you will probably want to cherry-pick the last commit on the mainline on top of that, unless that's solely related to the commit you want to remove. (edit2): Final note: if you want to avoid people pushing crap you don't want in your mainline, start doing pull requests and deny those people access to your mainline. (edit3): Forgot to mention that you'll have to instruct your coworkers to reset their work too. In this case I'd instruct them to create a separate branch and start a PR with their work, so you (both) have the opportunity to clean things up within that branch before merging to mainline. That avoids them losing their work and/or screwing up their local working tree. More on reddit.com
🌐 r/git
8
5
January 29, 2024
How do I get back to the most recent push/commit?
git reset HEAD~1 (note that's a tilde). You could also provide the hash of the previous commit as determined by git log -2. This will move any tracked files in the commit to the modified state, and any previously untracked files will be returned to the untracked state. Once you've fixed up the state of your code, you can git push --force to forcefully update upstream. Note this will lose the history of the unfortunate commit. If you don't want to rewrite history, you can perform a git revert HEAD to "undo" the previous commit as a new commit, and then manually fix up the code. This will leave the bunk commit as part of the history. If you know nobody else is working with your repo, rewriting history isn't a huge deal. More on reddit.com
🌐 r/learnprogramming
4
0
August 31, 2022
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
People also ask

What happens if I encounter conflicts while reverting a commit?
Resolve the conflicts manually, stage the changes, and then run `git revert --continue` to complete the revert operation.
🌐
blog.openreplay.com
blog.openreplay.com › openreplay blog › undoing git commits after push: safely revert changes on remote repositories
Undoing Git Commits After Push: Safely Revert Changes on Remote ...
🌐
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 - If you’re seeing this message ... that you’ve exited the terminal, you can finalize the process by running the command git push. After running this command, you’ve successfully reverted your commit....
🌐
OpenReplay
blog.openreplay.com › openreplay blog › undoing git commits after push: safely revert changes on remote repositories
Undoing Git Commits After Push: Safely Revert Changes on Remote Repositories
November 30, 2024 - # Make necessary changes git add . git commit --amend · When you need to undo commits that have already been pushed to a remote repository, you should use git revert to create a new commit that undoes the changes.
🌐
Reddit
reddit.com › r/git › how to undo pushed commits
r/git on Reddit: How to undo pushed commits
January 29, 2024 -
  • One of our artists managed to push 1 month of progress without merging

  • As you can see I tried to undo the commits one by one in reverse order

  • I couldn't undo the merge commit of Arvincle because it's empty

  • When I try to revert the commit before that I get one file I need to merge first, but the rest of the changes are not undone

Is there a way to reset the HEAD of main to the last valid commit?

🌐
DataCamp
datacamp.com › blog › git-undo-last-commit
Git Undo Last Commit: Step-by-Step Guide for Beginners | DataCamp
June 23, 2025 - Oops. Now you want to undo one of those commits without messing up the shared history. That’s where git revert saves the day. Run git revert HEAD~1. This creates a new commit that undoes the changes from the second-to-last commit (because HEAD~1 points to it).
Find elsewhere
🌐
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 - Avoid rewriting history with git reset after pushing the commits on main or other shared branches, as it can cause conflicts if teammates have already pulled the changes. Use git revert instead, which safely undoes changes while preserving history.
🌐
TheServerSide
theserverside.com › video › Undo-and-revert-pushed-Git-commits
Undo and revert pushed Git commits | TheServerSide
When the git revert command is issued without any other parameters, it undoes the changes that were part of the previous commit and creates a new commit to denote the change. When you push back to the server, anyone who looks at your latest commit will not see the code that was part of the ...
🌐
YouTube
youtube.com › cameron mckenzie
How to Undo a Pushed Git Commit - Reset & Revert a Git Commit After Push - YouTube
Need to undo a pushed Git commit from GitHub, GitLab, Bitbucket or CodeCommit? Well, there are two ways to revert a pushed commit in git.You can revert a com...
Published   January 31, 2024
🌐
KodeKloud
kodekloud.com › blog › git-uncommit-last-commit
How to Uncommit Last commit in Git (5 Scenarios)
November 25, 2025 - To undo everything, including discarding your changes and resetting your files to the previous commit, use git reset --hard HEAD. To undo a commit that you have already pushed to a remote branch, use git revert <commit_hash> --no-edit.
🌐
GitHub
gist.github.com › gunjanpatel › 18f9e4d1eb609597c50c2118e416e6a6
Git HowTo: revert a commit already pushed to a remote repository · GitHub
Sometimes you may want to undo a whole commit with all changes. Instead of going through all the changes manually, you can simply tell git to revert a commit, which does not even have to be the last one. Reverting a commit means to create a new commit that undoes all changes that were made in the bad commit.
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-undo-pushed-commits-using-git
How To Undo Pushed Commits Using Git? - GeeksforGeeks
June 27, 2024 - Undoing older commits: There are older commits in the history that you need to remove or modify without affecting the recent commits. The git revert command is used to create a new commit that undoes the changes made by previous commits.
🌐
Scaler
scaler.com › home › topics › git › how to revert last git commit?
How To Revert Last Git Commit? - Scaler Topics
March 9, 2023 - The revert command creates a commit that reverts changes in the target commit · The Git revert command works by creating a new commit containing the changes introduced by reverting the last commit. Git reset is another way to undo the last ...
🌐
Medium
lucaberton.medium.com › how-to-revert-the-latest-commit-in-git-a-complete-guide-adc12f396388
🌀 How to Revert the Latest Commit in Git: A Complete Guide | by Luca Berton | Medium
October 8, 2025 - If you’ve already pushed the commit to a shared branch (like main or develop), you should not rewrite history. Instead, use the git revert command: ... This creates a new commit that reverses the changes introduced by the last one.
🌐
Black Girl Bytes
blackgirlbytes.dev › how-to-undo-pushed-commits-with-git
How to Undo Pushed Commits with Git
April 5, 2022 - If you’re seeing this message ... that you’ve exited the terminal, you can finalize the process by running the command git push. After running this command, you’ve successfully reverted your commit....
🌐
Quora
quora.com › How-do-I-undo-a-pushed-commit-in-GitHub
How to undo a pushed commit in GitHub - Quora
Use git filter-repo (recommended) ... force-push; also rotate any exposed secrets. ... After rewriting, inform collaborators to reclone or run recovery instructions. 4) Undo a commit on a pull request branch without rewriting history · Create a new commit that removes the changes (git revert as above) ...
🌐
DEV Community
dev.to › isabelcmdcosta › how-to-undo-the-last-commit--31mg
Git Revert Pushed Commit: How to undo the last commit - DEV Community
February 20, 2018 - If you want to revert the last commit just do git revert <unwanted commit hash>; then you can push this new commit, which undid your previous commit.
🌐
Reddit
reddit.com › r/git › undoing last commit
Undoing last commit : r/git
January 7, 2021 - You'll have to use git push --force. If you don't want those changes anymore at all, you can use git reset --hard HEAD~1. Beware this is a destructive action, you bad commit will disappear entirely. ... This is the tutorial git: how to undo last commit that helped me.