git reset --hard HEAD~1
git push -f <remote> <branch>

(Example push: git push -f origin bugfix/bug123)

This will undo the last commit and push the updated history to the remote. You need to pass the -f because you're replacing upstream history in the remote.

Edit:

Please note that --hard will make your commit unreachable (i.e. it will appear to be deleted, but you can still git show <hash> or git log <hash> it if you remember its hash). If you want to keep your changes, run:

git reset [--mixed] HEAD~1

At this point you have unstaged changes because you used --mixed, which is the default.

You may first want to update the remote tree first (i.e. remove the commit): git push -f <remote> <branch>

Since you still have your changes locally you can create another branch and commit them there (and push as you see fit).

Answer from Alexander GroรŸ on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ git โ€บ how-to-delete-last-commit-in-git
How to Delete Last Commit in Git? - GeeksforGeeks
July 23, 2025 - Avoid force-pushing on shared branches: If you're working with others, be careful when using git push --force, as it can rewrite history for everyone. Backup your work: Before making big changes, itโ€™s always a good idea to create a backup ...
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
version control - How do I undo the most recent local commits in Git? - Stack Overflow
I accidentally committed the wrong files to Git but haven't pushed the commit to the server yet. How do I undo those commits from the local repository? More on stackoverflow.com
๐ŸŒ stackoverflow.com
git - Remove last commit and push - Stack Overflow
Learning GIT in general and GitKraken at the same time. I have done small change in in one file - aa.cpp , have commit and push to remote repository with the help of GitKraken. Suddenly I found tha... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Delete last Git commit from local and remote repositories.
Find answers to Delete last Git commit from local and remote repositories. from the expert community at Experts Exchange More on experts-exchange.com
๐ŸŒ experts-exchange.com
August 1, 2022
๐ŸŒ
GitHub
gist.github.com โ€บ CrookedNumber โ€บ 8964442
git: Removing the last commit ยท GitHub
... To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits.
๐ŸŒ
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 - Letโ€™s recreate a situation where you need to undo a commit after you push. In your repository, create a new file called index.md in our repository. Letโ€™s commit and push some changes to our repository. I added a few arbitrary changes for the sake of the tutorial. In the image below, I added the words โ€œhey, thereโ€ on line 1 of our index.md. Then I ran the following commands to add, commit, and push the changes. git add index.md git commit -m "added a greeting" git push
๐ŸŒ
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
๐ŸŒ
2coffee
2coffee.dev โ€บ en โ€บ articles โ€บ how-to-delete-a-pushed-commit
How to Delete a Commit That Has Been Pushed to a Remote Repository
January 5, 2024 - Suppose the remote develop branch has the last commit that you want to delete; great, take advantage of the time when no one has pulled it and no one has pushed anything new to force push. # Go back to the previous commit $ git reset HEAD~1 # Force ...
Find elsewhere
๐ŸŒ
Continuously Merging
articles.mergify.com โ€บ git-delete-last-commit
How to Git Delete Last Commit Safely
November 10, 2025 - But what if you've already pushed those commits? In that case, you have to use git revert. You'll need to revert each commit one by one, starting with the most recent one you want to undo and working your way backward.
๐ŸŒ
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?

๐ŸŒ
KodeKloud
kodekloud.com โ€บ blog โ€บ git-uncommit-last-commit
How to Uncommit Last commit in Git (5 Scenarios)
November 25, 2025 - To undo a commit that you have already pushed to a remote branch, use git revert <commit_hash> --no-edit. For visual learners, we've created this comprehensive video overview: Git allows you to create snapshots of your project called commits.
๐ŸŒ
BelieveMy
believemy.com โ€บ en โ€บ r โ€บ how-to-delete-a-git-commit
How to delete a Git commit?
December 5, 2024 - Find out how to remove a Git commit easily with methods such as git reset, git revert or git rebase.
๐ŸŒ
Medium
medium.com โ€บ @saijeevanreddy04 โ€บ delete-last-pushed-commit-in-git-100f15d8963b
How to Delete last pushed commit in GIT/GITHUB | by Saijeevanreddy | Medium
October 17, 2023 - To revert the pushed commit we just need to execute 2 simple commands. ... If not then checkout to correct branch in which you want to delete the commits. ... Note: The above command is used to delete only the last commit thats has been done on the specific branch, if you want to delete the older commit please follow this link.
๐ŸŒ
Daniel Abrahamberg
abrahamberg.com โ€บ home โ€บ git remove commits from branch after push: reset, revert, or rebase
Git remove commits from branch after push: reset, revert, or rebase
March 28, 2024 - For other team members ,to get ... been pushed to a remote repository, the most commonly used and safest method is to revert the commits....
Top answer
1 of 16
30140

Undo a commit & redo

$ git commit -m "Something terribly misguided" # (0: Your Accident)
$ git reset HEAD~                              # (1)
# === If you just want to undo the commit, stop here! ===
[ edit files as necessary ]                    # (2)
$ git add .                                    # (3)
$ git commit -c ORIG_HEAD                      # (4)
  1. git reset is the command responsible for the undo. It will undo your last commit while leaving your working tree (the state of your files on disk) untouched. You'll need to add them again before you can commit them again.
  2. Make corrections to working tree files.
  3. git add anything that you want to include in your new commit.
  4. Commit the changes, reusing the old commit message. reset copied the old head to .git/ORIG_HEAD; commit with -c ORIG_HEAD will open an editor, which initially contains the log message from the old commit and allows you to edit it. If you do not need to edit the message, you could use the -C option.

Alternatively, to edit the previous commit (or just its commit message), commit --amend will add changes within the current index to the previous commit.

To remove (not revert) a commit that has been pushed to the server, rewriting history with git push origin main --force[-with-lease] is necessary. It's almost always a bad idea to use --force; prefer --force-with-lease instead, and as noted in the git manual:

You should understand the implications of rewriting history if you amend a commit that has already been published.


Further Reading

You can use git reflog to determine the SHA-1 for the commit to which you wish to revert. Once you have this value, use the sequence of commands as explained above.


HEAD~ is the same as HEAD~1. The article What is the HEAD in git? is helpful if you want to uncommit multiple commits.

2 of 16
13009

Undoing a commit is a little scary if you don't know how it works. But it's actually amazingly easy if you do understand. I'll show you the 4 different ways you can undo a commit.

Say you have this, where C is your HEAD and (F) is the state of your files.

   (F)
A-B-C
    โ†‘
  master

Option 1: git reset --hard

You want to destroy commit C and also throw away any uncommitted changes. You do this:

git reset --hard HEAD~1

The result is:

 (F)
A-B
  โ†‘
master

Now B is the HEAD. Because you used --hard, your files are reset to their state at commit B.

Option 2: git reset

Maybe commit C wasn't a disaster, but just a bit off. You want to undo the commit but keep your changes for a bit of editing before you do a better commit. Starting again from here, with C as your HEAD:

   (F)
A-B-C
    โ†‘
  master

Do this, leaving off the --hard:

git reset HEAD~1

In this case the result is:

   (F)
A-B-C
  โ†‘
master

In both cases, HEAD is just a pointer to the latest commit. When you do a git reset HEAD~1, you tell Git to move the HEAD pointer back one commit. But (unless you use --hard) you leave your files as they were. So now git status shows the changes you had checked into C. You haven't lost a thing!

Option 3: git reset --soft

For the lightest touch, you can even undo your commit but leave your files and your index:

git reset --soft HEAD~1

This not only leaves your files alone, it even leaves your index alone. When you do git status, you'll see that the same files are in the index as before. In fact, right after this command, you could do git commit and you'd be redoing the same commit you just had.

Option 4: you did git reset --hard and need to get that code back

One more thing: Suppose you destroy a commit as in the first example, but then discover you needed it after all? Tough luck, right?

Nope, there's still a way to get it back. Type this

git reflog

and you'll see a list of (partial) commit SHAs (that is, hashes) that you've moved around in. Find the commit you destroyed, and do this:

git checkout -b someNewBranchName shaYouDestroyed

You've now resurrected that commit. Commits don't actually get destroyed in Git for some 90 days, so you can usually go back and rescue one you didn't mean to get rid of.

Top answer
1 of 3
43

If you have already pushed this commit, then it is possible that someone else has pulled the branch. In this case, rewriting your branch's history is undesirable and you should instead revert this commit:

git revert <SHA-1>
git push origin branch

Here <SHA-1> is the commit hash of the commit you want to remove. To find this hash value, simply type git log on your branch and inspect the first entry.

Using git revert actually adds a new commit which is the mirror image of the commit you want to remove. It is the preferred way of undoing a commit on a public branch because it simply adds new information to the branch.

If you are certain that you are the only person using this branch, then you have another option:

git reset --hard HEAD~1

followed by

git push --force origin branch

But you should only use this option if no one else is sharing this branch.

2 of 3
8

The way I go about it is by typing git status, which allows us to verify the branch we're currently on, followed by:

git log

Then, you'll see something like this:

commit aa09a82fb69af2d1aebde51d71514f7a03c3a692
Author: User <user@useremail.com>
Date:   Fri Nov 4 15:36:22 2016 -0400

    Fix issue with vertical scroll being forced

commit 411771837efe3ed555395e77fd35105a500ab758
Author: User <user@useremail.com>
Date:   Thu Nov 3 15:50:42 2016 -0400

    Add user notifications

commit f43b262f4e02b5a7268280e1230d44e36d1e547b
Author: User <user@useremail.com>
Date:   Thu Nov 3 12:11:00 2016 -0400

    All your base are belong to us

So, this tells us that commit aa09a82f is your last one, and commit 41177183 is the one before it, then:

git reset --hard 41177183

...brings us back to that commit, retaining the remote backup. With another git status to make sure that everything is all set for the double push (I'm personally a bit obsessive compulsive about verifying my current branch, especially when multitasking):

git push origin :<branch_name>
git push origin <branch_name>

At this point, you should be all set, but it's always good to follow that up with:

git fetch --all --prune
git branch -av

...which cleans up your branch list and shows you both local and remote to compare the commit messages.

Also, if working with a team, make sure that they're aware of this before moving forward. You don't want them to pull or push the branch on their end before you remove the last commit and push.

๐ŸŒ
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 - Next, if you want to undo your last local commit, use the git log command: The latest commit will have a commit hash (a long series of numbers and characters) and a (HEAD -> main) at the end โ€“ this is the commit you are looking to undo.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
Graphite
graphite.com โ€บ guides โ€บ how-to-delete-a-git-commit
How to delete a git commit - Graphite
If you've pushed a commit to a remote repository and want to remove it: ... This command deletes the most recent commit from your local repository, and discards the changes from that commit. ... Replace <branch-name> with your current branch. This ...
๐ŸŒ
TheServerSide
theserverside.com โ€บ video โ€บ Undo-and-revert-pushed-Git-commits
Undo and revert pushed Git commits | TheServerSide
When you push back to the server, ... undo. If you want to permanently delete a pushed Git commit from the server, you must perform a hard reset and then push back to the server with force....
๐ŸŒ
GitHub
gist.github.com โ€บ cutiko โ€บ 0b1615c63504a940877541362cc51211
Git delete last commit ยท GitHub
... To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits.