If nobody has pulled your remote repo yet, you can change your branch HEAD and force push it to said remote repo:

git reset --hard HEAD^ 
git push -f 

(or, if you have direct access to the remote repo, you can change its HEAD reference even though it is a bare repo)


Warning: git reset --hard discards local changes

As dremodaris correctly warns in the comments:

WARNING: the --hard option will also make you lose your changes in your local working directory!


Note, as commented by alien-technology in the comments below, on Windows (CMD session), you would need ^^:

git reset --hard HEAD^^
git push -f 

And, as noted in the comments by Jon Schneider:

If the command with "HEAD^" results in error no matches found: HEAD^, see "git show HEAD^ doesn't seem to be working. Is this normal?"

Update since 2011:
Using git push --force-with-lease (that I present here, introduced in 2013 with Git 1.8.5) is safer.

See Schwern's answer for illustration.


What if somebody has already pulled the repo? What would I do then?

Then I would suggest something that doesn't rewrite the history:

  • git revert locally your last commit (creating a new commit that reverses what the previous commit did)
  • push the 'revert' generated by git revert.
Answer from VonC on Stack Overflow
Top answer
1 of 15
450

If nobody has pulled your remote repo yet, you can change your branch HEAD and force push it to said remote repo:

git reset --hard HEAD^ 
git push -f 

(or, if you have direct access to the remote repo, you can change its HEAD reference even though it is a bare repo)


Warning: git reset --hard discards local changes

As dremodaris correctly warns in the comments:

WARNING: the --hard option will also make you lose your changes in your local working directory!


Note, as commented by alien-technology in the comments below, on Windows (CMD session), you would need ^^:

git reset --hard HEAD^^
git push -f 

And, as noted in the comments by Jon Schneider:

If the command with "HEAD^" results in error no matches found: HEAD^, see "git show HEAD^ doesn't seem to be working. Is this normal?"

Update since 2011:
Using git push --force-with-lease (that I present here, introduced in 2013 with Git 1.8.5) is safer.

See Schwern's answer for illustration.


What if somebody has already pulled the repo? What would I do then?

Then I would suggest something that doesn't rewrite the history:

  • git revert locally your last commit (creating a new commit that reverses what the previous commit did)
  • push the 'revert' generated by git revert.
2 of 15
82

Set the local branch one revision back (HEAD^ means one revision back):

git reset --hard HEAD^

Push the changes to origin:

git push --force

You will have to force pushing because otherwise git would recognize that you're behind origin by one commit and nothing will change.

Doing it with --force tells git to overwrite HEAD in the remote repo without respecting any advances there.

🌐
GitHub
gist.github.com › vishaltelangre › 5531806
Revert last local or remote commit (git) · GitHub
To restore everything back to the way it was prior to the last commit, we need to reset to the commit before HEAD: git reset --soft HEAD^ # use --soft if you want to keep your changes git reset --hard HEAD^ # use --hard if you don't care about ...
Discussions

Delete last Git commit from local and remote repositories.
I want to basically start over ... from the remote repository, and remove the commit from the local repository. Ideally, then I would be back to where I started originally, as if I had just created the branch. I have not yet run any commands. ... We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads. ... You can use the git revert command followed ... More on experts-exchange.com
🌐 experts-exchange.com
August 1, 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
Revert (merge) commits on remote repo
u/mrswats Thanks. It did work with "git revert". I was using the wrong commit id. More on reddit.com
🌐 r/git
6
0
August 7, 2021
How can I completely delete commits from both local and remote?
Yes. Your solution will work. git reset --hard [commit hash] will wipe everything after that commit. git push -f [origin branchname] Will make your remote like that too. Two precautions to take note of: This will clean out your directory. If you have any files in there that you want that are not part of previous commits you need to get them out. Using a git push -f without specifying origin branchname afterwards has the potential to force push commits on other branches or possibly overwrite your remote master branch if you are on a forked repository and you haven’t pulled in a bit. This can cause you headaches if you have pull requests out or other situations. It is always best to specify the branch name when using the force push. Additionally, take note of the command git reset --soft . This will allow you to revert your commit history in the same fashion as above while leaving the changes in your files as staged files. This is the solution if you don’t want your current commit history but you do want your current file changes. After this command, you can do a plain git reset to unstage these files or a git commit to make a new commit. More on reddit.com
🌐 r/git
3
5
May 11, 2020
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 ...
🌐
Reddit
reddit.com › r/git › undoing last commit
Undoing last commit : r/git
January 7, 2021 - You could delete the commit in your local devel branch, then push --force ... to the corresponding remote origin branch. That way your commit history is not cluttered with reverts ... You can revert as others have said, and I generally think that’s the best method.
🌐
Experts Exchange
experts-exchange.com › questions › 29244073 › Delete-last-Git-commit-from-local-and-remote-repositories.html
Solved: Delete last Git commit from local and remote repositories. | Experts Exchange
August 1, 2022 - You can also use git reset · git reset --soft or git reset --mixed or git reset --hard · Select allOpen in new windowBut this command is more dangerous, and I wouldn't recommend using it unless you really have to.
🌐
Medium
medium.com › @sharath.ravi › how-to-revert-a-git-commit-from-a-remote-repository-easy-step-by-step-tutorial-358f4a72fa1d
How to revert a git commit from a remote repository? Easy step-by-step tutorial. | by Sharath Ravi | Medium
March 21, 2020 - After this, if you check your repository status(using ‘git status’), you’ll notice that you have the HEAD detached at the commit you tested before. Then, push the new commit hash to a new branch in local (I created a local branch called ‘test-revert’) and push the branch to remote using commands:
Find elsewhere
🌐
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.
🌐
Adamdurrant
adamdurrant.co.uk › blog › undo-git-commit
How to Undo Pushed Git Commits Locally & Remotely<!-- --> | ADurrant
Now, you can refresh your remote repo and rejoice, you're back where you started, nobody suspects a thing. To undo consecutive commits from the top of your branch, in order, follow the same steps above but, for step 2, append the number of commits you wish to consecutively reset. For example the following command will undo the last 3 consecutive commits:
🌐
Ruegg
christoph.ruegg.name › blog › git-howto-revert-a-commit-already-pushed-to-a-remote-reposit
Git HowTo: revert a commit already pushed to a remote repository | Christoph Rüegg
May 5, 2010 - Where git interprets x^ as the parent of x and + as a forced non-fastforward push. If you have the master branch checked out locally, you can also do it in two simpler steps: First reset the branch to the parent of the current commit, then force-push it to the remote. Let's say the bad commit dd61ab32 is not the top commit, but a slightly older one, e.g. the second last one.
🌐
Edureka Community
edureka.co › home › community › categories › others › git revert last commit
git revert last commit | Edureka Community
February 8, 2022 - By mistake I had committed the wrong files to the Git but that didn't push the commit to the general ... out if there is a better way in doing so?
🌐
GitHub
gist.github.com › gunjanpatel › 18f9e4d1eb609597c50c2118e416e6a6
Git HowTo: revert a commit already pushed to a remote repository · GitHub
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. Just like above, the bad commit remains there, but it no longer affects the the current master and any future commits on top of it. ... Deleting the last commit is the easiest case. Let's say we have a remote origin with branch master that currently points to commit dd61ab32.
🌐
Git Skills
git-skills.dev › how-to › undo-last-commit
How to Undo Last Git Commits | Git Skills
To undo multiple last (local-only) commits, use this command (using the commit ID of the commit that should become HEAD): This will store all differences between the previous and the new HEAD into the Index. ... If you have already pushed your bad commits, they are now part of the remote repository.
🌐
Code with C
codewithc.com › code with c › blog › mastering git: reverting your last commit
Mastering Git: Reverting Your Last Commit - Code With C
February 10, 2024 - # Step 2: Revert the last commit using git revert git revert HEAD # This creates a new commit that undoes the changes made by the last commit. # Step 3: If you want to change the default commit message, you can use git revert HEAD --no-edit ...
🌐
freeCodeCamp
freecodecamp.org › news › git-remove-last-commit-how-to-undo-a-commit-in-git
Git Remove Last Commit – How to Undo a Commit in Git
September 21, 2022 - The command above will undo the changes by creating a new commit and reverting that file to its previous state, as if it never changed. Lastly, use git push to push the change to the remote branch.
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-undo-the-most-recent-local-commits-in-git
How to Undo the Most Recent Local Commits in Git? - GeeksforGeeks
July 23, 2025 - This example undoes the last three commits, keeping the changes staged. The `git revert` command is a safer alternative, especially when working with a shared repository, as it creates a new commit that undoes the changes from the specified commit.
🌐
Step2dev
step2.dev › en › blog › 425-git-revert-commit-how-to-undo-the-last-commit
Git Revert Commit - How To Undo The Last Commit - Step2dev
March 4, 2024 - If you want to reset to the last commit and also remove all unstaged changes, you can use the --hard option: ... This will undo the latest commit, but also any uncommitted changes.
🌐
DataCamp
datacamp.com › blog › git-undo-last-commit
Git Undo Last Commit: Step-by-Step Guide for Beginners | DataCamp
June 23, 2025 - 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). It doesn’t delete anything, instead it reverses the changes in a clean, trackable way. Once you’ve done that, don’t forget to push the fix to the remote ...
🌐
JetBrains
jetbrains.com › guide › tips › undo-last-commit
Undo your Last Commit - JetBrains Guide
February 17, 2023 - That commit is…gone! We’re back to where we were before the commit. What would this have been like from the command line? Let’s take a look at the Git tool’s Console tab, to see behind the scenes.
🌐
Atlassian
atlassian.com › git › tutorials › undoing changes › git revert
How to Revert a Commit in Git? | Atlassian Git Tutorial
A revert operation will take the specified commit, inverse the changes from that commit, and create a new "revert commit". The ref pointers are then updated to point at the new revert commit making it the tip of the branch. To demonstrate let’s create an example repo using the command line examples below: 1$ mkdir git_revert_test 2$ cd git_revert_test/ 3$ git init .
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-reset-remote-repository-to-a-certain-commit-in-git
How To Reset Remote Repository to a Certain Commit in Git? - GeeksforGeeks
June 26, 2024 - ... Step 3. Fetch Latest Changes: Fetch the latest changes from the remote repository. ... Step 4. Revert to the Desired Commit: Use the `git revert` command to create new commits that undo the changes.