There are a lot of ways to do so, for example:

in case you have not pushed the commit publicly yet:

git reset HEAD~1 --soft   

That's it, your commit changes will be in your working directory, whereas the LAST commit will be removed from your current branch. See git reset man


In case you did push publicly (on a branch called 'master'):

git checkout -b MyCommit //save your commit in a separate branch just in case (so you don't have to dig it from reflog in case you screw up :) )

revert commit normally and push

git checkout master
git revert a8172f36 #hash of the commit you want to destroy
# this introduces a new commit (say, it's hash is 86b48ba) which removes changes, introduced in the commit in question (but those changes are still visible in the history)
git push origin master

now if you want to have those changes as you local changes in your working copy ("so that your local copy keeps the changes made in that commit") - just revert the revert commit with --no-commit option:

git revert --no-commit 86b48ba (hash of the revert commit).

I've crafted a small example: https://github.com/Isantipov/git-revert/commits/master

Answer from Isantipov on Stack Overflow
Top answer
1 of 9
1774

There are a lot of ways to do so, for example:

in case you have not pushed the commit publicly yet:

git reset HEAD~1 --soft   

That's it, your commit changes will be in your working directory, whereas the LAST commit will be removed from your current branch. See git reset man


In case you did push publicly (on a branch called 'master'):

git checkout -b MyCommit //save your commit in a separate branch just in case (so you don't have to dig it from reflog in case you screw up :) )

revert commit normally and push

git checkout master
git revert a8172f36 #hash of the commit you want to destroy
# this introduces a new commit (say, it's hash is 86b48ba) which removes changes, introduced in the commit in question (but those changes are still visible in the history)
git push origin master

now if you want to have those changes as you local changes in your working copy ("so that your local copy keeps the changes made in that commit") - just revert the revert commit with --no-commit option:

git revert --no-commit 86b48ba (hash of the revert commit).

I've crafted a small example: https://github.com/Isantipov/git-revert/commits/master

2 of 9
56

The easiest way to undo the last Git commit is to execute the git reset command with one of the below options

  • soft
  • hard
  • mixed

Let's assume you have added two commits and you want to undo the last commit

$ git log --oneline

45e6e13 (HEAD -> master) Second commit
eb14168 Initial commit

–soft option undo the last commit and preserve changes done to your files

$ git reset --soft HEAD~1


$ git status

On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   file.html


$ git log --oneline

eb14168 (HEAD -> master) Initial commit

–hard option undo the last commit and discard all changes in the working directory and index

$ git reset --hard HEAD~1


$ git status

nothing to commit, working tree clean


$ git log --oneline

eb14168 (HEAD -> master) Initial commit

--mixed option undo the last commit and keep changes in the working directory but NOT in the index

$ git reset --mixed HEAD~1


$ git status

On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   file.html

no changes added to commit (use "git add" and/or "git commit -a")


$ git log --oneline

eb14168 (HEAD -> master) Initial commit
🌐
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?

Discussions

Remove a git commit which has not been pushed - Stack Overflow
I did a git commit but I have not pushed it to the repository yet. So when I do git status, I get '# Your branch is ahead of 'master' by 1 commit. So if I want to roll back my top commit, can I ju... More on stackoverflow.com
🌐 stackoverflow.com
What is the best way to revert a commit that is displaying on Bitbucket?

If multiple devs are working on master branch, it is not a good practice to delete commits. As in the meantime someone could have checkedout master and the history would not be intact anymore. In such case it is better to revert the commits and push again.

However if keeping the history visually clean is more important than keeping it intact, then like suggested above, you can reset and force push with —force-with-lease which only pushes after making sure no one else had updated the branch in the meantime.

From experience, people would hate you more for messing the history up, than for adding a couple of commits and their reverts.

Edit: code formatting

More on reddit.com
🌐 r/git
8
3
March 1, 2021
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
I have 3 commits that is not pushed. I need to change the 1st commit. What is the best way to do it?
Start an interactive rebase. git rebase -i HEAD~3 In the editor that opens, change the word pick to edit on the commit you want to change. Save and exit. The rebase will pause at that commit, so you can delete the file. git rm --cached Amend the commit. git commit --amend --no-edit Continue the rebase. git rebase --continue Caveat emptor. More on reddit.com
🌐 r/git
22
25
August 26, 2024
People also ask

Can I undo multiple commits before pushing?
Yes, you can undo multiple commits before pushing by specifying how many commits to reset. For example, `git reset --soft HEAD~3` moves back three commits, keeping all changes staged.nnIf you want to discard multiple commits and their changes, use `git reset --hard HEAD~3`. Be cautious with this command, as it permanently deletes the commits and their associated changes. This approach is useful for cleaning up a series of local commits before sharing your work.
🌐
ituonline.com
ituonline.com › blogs › how-to-undo-a-git-commit-before-pushing-to-remote-2
How To Undo A Git Commit Before Pushing To Remote – ITU Online ...
What is the difference between 'git reset' and 'git revert' when undoing commits?
`git reset` and `git revert` serve different purposes. `git reset` moves the current branch pointer to a previous commit, effectively rewriting history. It can be used to undo local commits before they are pushed.nnOn the other hand, `git revert` creates a new commit that reverses the changes introduced by a previous commit. This is often used to undo changes in shared history, especially after commits have been pushed to a remote repository. When working locally before pushing, `git reset` is usually preferred for its simplicity.
🌐
ituonline.com
ituonline.com › blogs › how-to-undo-a-git-commit-before-pushing-to-remote-2
How To Undo A Git Commit Before Pushing To Remote – ITU Online ...
🌐
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. These tools allow you to fix issues without permanently losing work. To unstage a file after git add, use git reset HEAD <file-name>...
🌐
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 - Avoid using git push --force to overwrite the remote repository’s history, as it can cause issues for other collaborators. Instead, use git revert to maintain a clean and transparent commit history.
🌐
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 ... 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 ...
Find elsewhere
🌐
ITU Online
ituonline.com › blogs › how-to-undo-a-git-commit-before-pushing-to-remote-2
How To Undo A Git Commit Before Pushing To Remote – ITU Online IT Training
June 16, 2026 - To undo a Git commit before pushing, use git commit –amend for the latest commit, git reset –soft HEAD~1 to keep changes staged, git reset –mixed HEAD~1 to keep changes unstaged, or git reset –hard HEAD~1 to discard everything.
🌐
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've made several commits and want to undo them all before pushing to the remote repository, you can use the git reset command: ## Undo the last 3 commits, but keep the changes in the working directory git reset HEAD~3 ## Undo the last ...
🌐
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 - To undo a Git commit before push, you can use the "git reset" command or the "git revert" command.
🌐
GitLab
docs.gitlab.com › topics › git › undo
Revert and undo changes | GitLab Docs
Git gives you control over your ... to undo changes at any point in your Git workflow. Recover from accidental commits, remove sensitive data, fix incorrect merges, and maintain a clean repository history. When collaborating with others, preserve transparency with new revert commits, or reset your work locally before sharing. The method to use depends on whether the changes are: Only on your local computer. Stored remotely on a Git server such as GitLab.com. Until you push your changes ...
🌐
Delft Stack
delftstack.com › home › howto › git › git undo commit before push
How to Undo a Commit Before Pushing Remote Repository in Git | Delft Stack
February 2, 2024 - Thus, to discard a commit present in the local repository and not yet pushed to the remote one, we can execute the git reset command as follows. ... The git reset command above resets the current HEAD by one commit.
🌐
GitKraken
gitkraken.com › home › learn › problems & solutions › undo git commit
Undo Git Commit | How do you undo your last Git commit?
August 5, 2022 - If you make a mistake with your last commit and wish to undo the last Git commit before you push, you can simply click the magical Undo button at the toolbar at the top of the UI.
🌐
Graphite
graphite.com › guides › undo-git-commit
How to undo a git commit
If you want to completely clean the staging area and discard all changes from the last N commits use the --hard flag: ... If you've already pushed your commit or want to undo a specific commit safely without rewriting history, git revert is the ...
🌐
GitHub
docs.github.com › en › desktop › managing-commits › undoing-a-commit-in-github-desktop
Undoing a commit in GitHub Desktop - GitHub Docs
You can undo multiple sequential commits up to a commit that has already been pushed to the remote repository by selecting a previous commit and using the "reset to commit" option.
🌐
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 - For example, to undo the last 2 commits (assuming both have not been pushed) run Git reset—soft HEAD~2 · NOTE: Git reset—soft HEAD~ is the same as Git reset—soft HEAD^ which you may see in Git documentation.
🌐
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 - One major benefit of version control is that you can roll back your code to any time in history without significantly disrupting your teammates. However, reverting your code isn’t always straightforward, especially when you’re still learning Git or gaining confidence navigating the command line. In this post, I will walk you through undoing a commit after you push your changes via the terminal.
🌐
Graphite
graphite.com › guides › git-undo-last-commit
Git undo last commit - Graphite
Be careful with git reset --hard: This command permanently discards changes, so use it with caution. If you only want to remove the commit but keep the changes in your working directory, use git reset --soft.
🌐
LabEx
labex.io › tutorials › git-how-to-undo-git-commit-but-keep-changes-392512
How to Undo Git Commit But Keep Changes | LabEx
Undoing a commit can have different ... revert, git commit --amend), and it's important to choose the right approach for your specific use case. If you're working on a collaborative project, it's essential to communicate with your team before ...
🌐
LabEx
labex.io › tutorials › git-how-to-undo-a-git-commit-that-has-not-been-pushed-417756
How to undo a Git commit that has not been pushed | LabEx
If you want to undo multiple commits or revert more significant changes, you can use the git reset command. This command allows you to move the branch pointer to a specific commit, effectively undoing all commits that came after that point.
🌐
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
Soft Reset: git reset --soft HEAD~1 - Remove the last commit but keep changes in the working directory. Interactive Rebase: git rebase -i HEAD~n - Remove a specific older commit. Amend Commit: git commit --amend - Modify the last commit.