IF you have NOT pushed your changes to remote

git reset HEAD~1

Check if the working copy is clean by git status.

ELSE you have pushed your changes to remote

git revert HEAD

This command will revert/remove the local commits/change and then you can push

Answer from Jeril Kuruvila on Stack Overflow
🌐
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 - You can use git reset to undo git commit before push​, git revert to safely undo commits that have already been pushed, and git reflog to recover lost commits or find previous states of your repository.
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
git reset - How can I undo pushed commits using Git? - Stack Overflow
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. ... Sign up to request clarification or add additional context in comments. ... In git documentation, it says that revert command reverts commits between first and last ... More on stackoverflow.com
🌐 stackoverflow.com
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
People also ask

How to Undo, Revert, or Delete a Git Commit
To undo the last local commit (one that hasn't been pushed yet) while keeping your changes staged, run git reset --soft HEAD~1. To unstage the changes but keep the edits in your working directory, use git reset --mixed HEAD~1. To discard the changes entirely, use git reset --hard HEAD~1 — this permanently deletes the uncommitted work. To undo a specific older commit without altering history, use git revert , which creates a new commit that applies the reverse of the targeted commit's changes; this is the safest approach for shared branches. The --no-commit flag stages the reverting changes wit
🌐
git-tower.com
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 ...
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 ...
🌐
Better Stack
betterstack.com › community › questions › how-to-remove-git-commit-not-pushed
Remove a Git Commit Which Has Not Been Pushed | Better Stack Community
Amend Commit: git commit --amend - Modify the last commit. These commands will help you remove or alter commits that have not been pushed to the remote repository.
🌐
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 - To undo multiple commits, you can use git revert with a range of commits or perform an interactive rebase. git revert <oldest-commit-hash>..<latest-commit-hash> Use git rebase -i <commit-before-the-one-you-want-to-change> to start an interactive ...
🌐
GitLab
docs.gitlab.com › topics › git › undo
Revert and undo changes | GitLab Docs
# Modify history from commit-id to HEAD (current commit) git rebase -i commit-id · When contributing to large open source repositories, consider squashing your commits into a single commit. This practice: Helps maintain a clean and linear project history. Simplifies the process of reverting ...
🌐
LabEx
labex.io › tutorials › git-how-to-undo-git-commits-before-pushing-to-remote-398128
How to Undo Git Commits Before Pushing to Remote | LabEx
If you want to undo multiple commits before pushing to the remote, you can use the git reset command. This will move the branch pointer back to the specified commit, effectively undoing all the commits after that point.
Find elsewhere
🌐
Squash
squash.io › how-to-undo-a-git-commit-before-push
How to Undo/Revert a Git Commit Before Push - Squash Labs
October 28, 2023 - By squashlabs, Last Updated: Oct. 28, 2023 ... To undo a Git commit before push, you can use the "git reset" command or the "git revert" command.
🌐
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
When you want to undo your last local commit — one that hasn't been pushed to a remote yet — git reset is the right tool: ... This rewinds the branch pointer by one commit. The --soft flag preserves your changes as staged modifications, ...
Published   4 days ago
🌐
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?

🌐
Numla
numla.com › blog › odoo-development-18 › how-to-undo-git-add-and-git-commit-before-push-220
How to Undo git add and git Commit Before Push | Numla
May 3, 2025 - Sometimes, you may find yourself in a situation where you've added and committed changes in Git, but you realise you need to undo those actions before pushing them to the remote repository. This guide walks you through the steps to unstage files and undo commits in Git. ... If you’ve already committed the changes but haven’t pushed, you can undo the commit. The changes will remain in your working directory, so you can modify them or add more changes. To undo the last commit but keep the changes in your working directory:
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 › 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.
🌐
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 ...
🌐
Pantheon
docs.pantheon.io › guides › git › undo-commits
Undo Git Commits | Pantheon Docs
Revert your code back to the commit before core was overwritten. In this example, before commit 9a11sd8f67af9679a6fsafasdf802834207489328, where changes were made on Date: Fri Dec 6 15:37:24 2014 -0700. Apply any changes you have made since the date core was overwritten. Updating each file with a copy from a backup is the best option. You can delete changes made in your local environment that have not been pushed to Pantheon.
🌐
Graphite
graphite.com › guides › git-undo-last-commit
Git undo last commit - Graphite
If you need to undo a pushed commit, consider using git revert instead, which creates a new commit that undoes the changes of the commit you want to discard. Q: What's the difference between git reset --soft HEAD~ and git reset --hard HEAD~? ...
🌐
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 ...
🌐
CSNAINC
csnainc.io › programming languages › undo a git push – how to revert, reset, or recover commits
Undo a Git Push - How to Revert, Reset, or Recover Commits - CSNAINC
May 11, 2025 - Before pushing or merging, you might want to clean things up. That’s where interactive rebase comes in. Here’s the command: It opens a list of your last 4 commits. You can: ... Only use this on local branches where history rewriting won’t mess with others. Scenario 1: You pushed a broken commit to main → Use git revert and push the fix
🌐
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.
🌐
Nobledesktop
blog.nobledesktop.com › learn › git › undo changes in git: git checkout, git revert, & git reset
Undo Changes in Git: checkout, revert, & reset
April 19, 2026 - If you have made local commits ... not been pushed yet you can reset things back to a previous good commit. It will be as if the bad commits never happened. Here's how: In your terminal (Terminal, Git Bash, or Windows Command Prompt), navigate to the folder for your Git repo. Run Git status and make sure you have a clean working tree. Each commit has a unique hash (which looks something like 2f5451f). You need to find the hash for the last good commit (the one you want to revert back ...
🌐
Warp
warp.dev › terminus by warp › git › undo a git push
How to undo a git push | Warp
January 31, 2024 - Alternatively, if you don’t want ... the history of the remote repository with the last known good commit (identified by using git log) from the specified branch name....