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

🌐
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....
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
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
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
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 ...
🌐
Graphite
graphite.com › guides › git-revert-commit-after-pushing
git revert commit after pushing
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 ...
🌐
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?

🌐
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.
Find elsewhere
🌐
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).
🌐
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.
🌐
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.
🌐
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.
🌐
Quora
quora.com › How-do-I-undo-the-last-Git-commit
How to undo the last Git commit - Quora
Answer (1 of 7): First thing, question doesn't say from where you want to revert. From remote or local repository. I am assuming your question is more general since it says "all possible ways" and asking me from both repositories. * Your commit is not pushed to the remote repository. 1. git re...
🌐
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.
🌐
Amp
ampcode.com › manual
Owner’s Manual - Amp
The repository can be an existing one — on GitHub or any Git URL — or a new one that Amp creates and hosts. Clone Amp-hosted repositories with amp clone owner/project-name (owner is the user or workspace name). The Changes Workflow setting picks the main action in a thread’s changes sidebar: Ship commits and pushes directly to origin/main; Push to Branch pushes the current branch and, on GitHub, returns a pull request URL.
🌐
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.
🌐
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
🌐
Quora
quora.com › How-do-I-undo-a-pushed-commit-in-GitHub
How to undo a pushed commit in GitHub - Quora
Answer (1 of 3): 1. go back in history, but preserve history: [code ]revert[/code] 2. go back in history and alter history, so the content of the commits is removed forever: [code ]reset --hard[/code] and [code ]push --force[/code]. If you're not sure, then use [code ]revert[/code], it's the safe...
🌐
Git
git-scm.com › docs › git-worktree
Git - git-worktree Documentation
$ git worktree add -b emergency-fix ../temp master $ pushd ../temp # ... hack hack hack ... $ git commit -a -m 'emergency fix for boss' $ popd $ git worktree remove ../temp
🌐
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 ...
🌐
Warp
warp.dev › terminus by warp › git › undoing git commits
How To Undo Your Last Git Commit(s) | Warp
November 30, 2023 - If you decide to use git reset or git checkout to overwrite commits, then you can use git push --force to overwrite the remote branch with your local branch. This will overwrite any commits that were made after your last ...