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
🌐
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.
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
How to un-commit all un-pushed git commits without losing the changes - Stack Overflow
Is there any way to undo all un-pushed commits (I made 3 commits to the wrong branch without pushing) without losing the changes? More on stackoverflow.com
🌐 stackoverflow.com
How do I undo the last commit without losing changes?
just committed but realized I need to edit some files before finalizing. How can I undo the last commit but keep my changes in the files? More on github.com
🌐 github.com
3
2
Can I delete (or undo) a git commit but keep the changes? - Stack Overflow
In my case, I wanted to revert ... already pushed to the repo and already had other commits following it, but still keep its changes to build upon them. The git revert -n suggested by @WimFeijen didn't really do that. ... What does this do? This reverts the commit normally. The cherry-pick of the same commit adds and stages all the changes again without a commit. The following restore unstages all changes so I can stage everything I need myself. Not as ... More on stackoverflow.com
🌐 stackoverflow.com
People also ask

How do I undo a commit that has already been pushed?
If the commit has already been pushed to a remote repository, the safest approach is to use `git revert `. This creates a new commit that reverses the changes without rewriting history.
🌐
golinuxcloud.com
golinuxcloud.com › home › devops › git › how to remove a commit in git (undo, delete or revert) with examples
How to Remove a Commit in Git (Undo, Delete or Revert) with Examples ...
How can I recover a deleted Git commit?
If you accidentally remove a commit, you can recover it using `git reflog`. Find the commit hash in the reflog history and restore it using `git checkout ` or `git reset --hard `.
🌐
golinuxcloud.com
golinuxcloud.com › home › devops › git › how to remove a commit in git (undo, delete or revert) with examples
How to Remove a Commit in Git (Undo, Delete or Revert) with Examples ...
How do I remove the last commit in Git?
You can remove the last commit using `git reset --soft HEAD~1` to keep changes staged, `git reset --mixed HEAD~1` to keep changes in the working directory, or `git reset --hard HEAD~1` to permanently delete the commit and its changes.
🌐
golinuxcloud.com
golinuxcloud.com › home › devops › git › how to remove a commit in git (undo, delete or revert) with examples
How to Remove a Commit in Git (Undo, Delete or Revert) with Examples ...
🌐
Reddit
reddit.com › r/git › new to git, advice needed. revert commit but keep changes
r/git on Reddit: New to git, advice needed. Revert commit but keep changes
November 18, 2023 -

Hello everyone! I'm working on a project in a team. I'm using GUI git client Fork, while also learning command line git.
I finished a task and made a commit (not pushed), but soon realized that I didn't get it done properly, and that I needed to put more work into it. So I left my initial commit as it was, and just kept on working on the task. Right now I'm finally finishing it, but I'm asked to make only one commit with the whole task in it.

When I try to Revert the initial commit, it gives me an error:

Your local changes to the following files would be overwritten by merge
Revert failed

What should I do to keep the changes in the initial commit and reapply the new ones? I may have rewritten some code from the initial commit, but not fully, so some parts must stay, and the new changes to be added.

Thank you for your attention!

🌐
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.
🌐
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 - These tools allow you to fix issues without permanently losing work. To unstage a file after git add, use git reset HEAD <file-name>. This moves the file back to the working directory but keeps the changes.
Find elsewhere
🌐
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.
🌐
Mazer.dev
mazer.dev › en › git › tips › how-uncommit-undo-last-commit-not-pushed
How to Use Git Uncommit to Revert Changes - Mazer.dev
April 20, 2023 - Using git reset --soft HEAD~ you can undo commit and keep all files staged. This is the lightest touch, you can even undo your commit but leave your files and your index: ... This not only leaves your files alone, it even leaves your index alone.
🌐
GoLinuxCloud
golinuxcloud.com › home › devops › git › how to remove a commit in git (undo, delete or revert) with examples
How to Remove a Commit in Git (Undo, Delete or Revert) with Examples | GoLinuxCloud
March 16, 2026 - Sometimes you commit changes but realize the commit should not be pushed yet. In this case, it is safe to undo the commit locally. ... This moves the changes back to the working directory.
🌐
SheCanCode
shecancode.io › blog › how-to-undo-a-commit-in-github
How To Undo a Commit in GitHub — SheCanCode
December 15, 2021 - The last step is to push your changes to the remote branch: To undo a commit that has not been pushed to a remote repository, use the reset command. Note that the reset command should be used if the commits only exist locally.
Top answer
1 of 14
3396

It's as simple as this:

git reset HEAD^

Note: some shells treat ^ as a special character (for example some Windows shells or ZSH with globbing enabled), so you may have to quote "HEAD^" or use HEAD~1 in those cases.

git reset without a --hard or --soft moves your HEAD to point to the specified commit, without changing any files. HEAD^ refers to the (first) parent commit of your current commit, which in your case is the commit before the temporary one.

Note that another option is to carry on as normal, and then at the next commit point instead run:

git commit --amend [-m … etc]

which will instead edit the most recent commit, having the same effect as above.

Note that this (as with nearly every git answer) can cause problems if you've already pushed the bad commit to a place where someone else may have pulled it from. Try to avoid that

2 of 14
430

There are two ways of handling this. Which is easier depends on your situation.

Reset

If the commit you want to get rid of was the last commit, and you have not done any additional work you can simply use git-reset

git reset HEAD^

Takes your branch back to the commit just before your current HEAD. However, it doesn't actually change the files in your working tree. As a result, the changes that were in that commit show up as modified - its like an 'uncommit' command. In fact, I have an alias to do just that.

git config --global alias.uncommit 'reset HEAD^'

Then you can just used git uncommit in the future to back up one commit.

Squashing

Squashing a commit means combining two or more commits into one. I do this quite often. In your case you have a half done feature commited, and then you would finish it off and commit again with the proper, permanent commit message.

git rebase -i <ref>

I say above because I want to make it clear this could be any number of commits back. Run git log and find the commit you want to get rid of, copy its SHA1 and use it in place of <ref>. Git will take you into interactive rebase mode. It will show all the commits between your current state and whatever you put in place of <ref>. So if <ref> is 10 commits ago, it will show you all 10 commits.

In front of each commit, it will have the word pick. Find the commit you want to get rid of and change it from pick to fixup or squash. Using fixup simply discards that commits message and merges the changes into its immediate predecessor in the list. The squash keyword does the same thing, but allows you to edit the commit message of the newly combined commit.

Note that the commits will be re-committed in the order they show up on the list when you exit the editor. So if you made a temporary commit, then did other work on the same branch, and completed the feature in a later commit, then using rebase would allow you to re-sort the commits and squash them.

WARNING:

Rebasing modifies history - DONT do this to any commits you have already shared with other developers.

Stashing

In the future, to avoid this problem consider using git stash to temporarily store uncommitted work.

git stash save 'some message'

This will store your current changes off to the side in your stash list. Above is the most explicit version of the stash command, allowing for a comment to describe what you are stashing. You can also simply run git stash and nothing else, but no message will be stored.

You can browse your stash list with...

git stash list

This will show you all your stashes, what branches they were done on, and the message and at the beginning of each line, and identifier for that stash which looks like this stash@{#} where # is its position in the array of stashes.

To restore a stash (which can be done on any branch, regardless of where the stash was originally created) you simply run...

git stash apply stash@{#}

Again, there # is the position in the array of stashes. If the stash you want to restore is in the 0 position - that is, if it was the most recent stash. Then you can just run the command without specifying the stash position, git will assume you mean the last one: git stash apply.

So, for example, if I find myself working on the wrong branch - I may run the following sequence of commands.

git stash
git checkout <correct_branch>
git stash apply

In your case you moved around branches a bit more, but the same idea still applies.

🌐
GitKraken
gitkraken.com › home › learn › problems & solutions › undo git commit
Undo Git Commit | How do you undo your last Git commit?
August 5, 2022 - To undo a Git commit after you’ve performed another action in GitKraken, you will have to use the Git reset feature. Simply right-click on the commit from the central graph and select Reset -> Soft to keep all of the changes...
🌐
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?

🌐
GitLab
docs.gitlab.com › topics › git › undo
Revert and undo changes | GitLab Docs
You can undo local changes that are already staged. In the following example, a file was added to the staging, but not committed: ... On branch main Your branch is up-to-date with 'origin/main'. Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: <file> ... When you commit to your local repository with git commit, Git records your changes. Because you did not push ...
🌐
Medium
rajputankit22.medium.com › remove-git-commit-which-has-not-been-pushed-c95dfb2c3209
Remove git commit which has not been pushed | by Ankit Kumar Rajpoot | Medium
August 29, 2020 - // Add file $ git add validator.js// Commit code $ git commit -m "change some validations" ... Now, we will check the status through the following command. ... Now, we will undo the commit. So there are multiple methods for undo, we will perform one by one. Before performing the different methods we should know about logs. Use git log to check all commits then you can decide how many commits do you want to roll back. ... There are three ways to remove commits. For keeping files in the staged so we should have to use the following command.
🌐
Reliable Penguin
blogs.reliablepenguin.com › home › undoing local git commits you haven’t pushed (safely)
Undoing local Git commits you haven’t pushed (safely) Undo Unpushed Git Commits Safely (Exact Commands)
October 10, 2025 - Use when: You know how many commits you want to undo from HEAD and you may want to keep (or drop) the changes. Replace N with the number of commits to remove, e.g., HEAD~2. If you’re mid‑operation, bail out cleanly first: Then apply the appropriate reset option above. See the local commits that are ahead of the remote: ... Show what’s staged vs. unstaged: “I committed to the wrong branch, nothing pushed.”
🌐
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.
🌐
Reddit
reddit.com › r/git › i have 3 commits that is not pushed. i need to change the 1st commit. what is the best way to do it?
r/git on Reddit: I have 3 commits that is not pushed. I need to change the 1st commit. What is the best way to do it?
August 26, 2024 -

I accidentally included a key file in the "doc update" commit so I need to remove it before pushing to the server. However, there are 2 more commits after that one. I know I could cancel the commits and make them all as working tree changes. I just wonder if there is a better way to preserve the last 2 commits.

P.S. this is a personal project so I am the only one who use this. That's why I have 3 commits queued up here. I know its a bad move. If I push my last commit before starting a new one, I could have avoid this situation.