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

Answer from Gareth on Stack Overflow
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.

Discussions

Is it possible to undo the last Git commit but keep the code changes? - Ask a Question - TestMu AI (formerly LambdaTest) Community
I ran into a workflow situation with Git and I’m trying to clean things up without losing any work. Here’s the scenario: I was working on a development branch and made several local changes. Before I could finish the feature, I needed to switch over to master to demo something. More on community.testmuai.com
🌐 community.testmuai.com
0
December 22, 2025
4 Ways to Undo a Git Commit - Amend vs Reset
what about rebasing to edit, squash, fix up, drop, or reorder commits interactively? More on reddit.com
🌐 r/opensource
3
8
January 12, 2023
Undo multiple commits but keep local changes.

You can reset the head on your checked out branch to any hash you'd like in your history. It won't make changes to the files in your workspace without the -hard flag, so just do a reset to whatever commit you'd like, without passing it -hard.

This, of course, ignores whatever "Intellij" is, but you asked a question in "r/git", and that's how what you asked is addressed with Git.

More on reddit.com
🌐 r/git
9
6
April 27, 2021
New to git, advice needed. Revert commit but keep changes
You can either reset, as u/large_crimson_canine suggested, or you can make your new changes, git add them and then amend the commit you already made with: git --amend or, to retain the previous commit message: git --amend --no-edit More on reddit.com
🌐 r/git
17
0
November 18, 2023
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 › 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!

🌐
Graphite
graphite.com › guides › git-undo-last-commit
Git undo last commit
... 2. Undo the last commit but keep the changes: If you want to undo the commit but keep the changes in your working directory, use the git reset command with the --soft flag, followed by HEAD~.
🌐
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
🌐
CloudBees
cloudbees.com › blog › git-undo-commit
How to Undo Changes in Git with Git Undo Commit
November 25, 2020 - If you must undo changes in a shared branch of a shared repo, the best tool to use is git revert <commit id>. It reverts the changes done by the commit you specified, and then it creates a new commit for it.
🌐
KodeKloud
kodekloud.com › blog › git-uncommit-last-commit
How to Uncommit Last commit in Git (5 Scenarios)
November 25, 2025 - You want to undo your commit, but you don’t want to lose your changes or your staging. What do you do? The answer is simple: use the git reset --soft HEAD~1 command. This command will undo your last commit, but it will keep your changes and ...
🌐
GitLab
docs.gitlab.com › topics › git › undo
Revert and undo changes | GitLab Docs
For more information about purging ... and you want to change it back to how it was in the previous commit, but keep the commit history, you can use git revert....
🌐
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 - Be cautious when using git reset --hard, as it permanently deletes changes. Regularly communicate with your team to avoid conflicts when undoing commits on shared branches. ... Use `git revert -m 1 <merge-commit-hash>` to revert a merge commit, ...
🌐
TestMu AI
community.testmuai.com › ask a question
Is it possible to undo the last Git commit but keep the code changes? - Ask a Question - TestMu AI (formerly LambdaTest) Community
December 22, 2025 - I ran into a workflow situation with Git and I’m trying to clean things up without losing any work. Here’s the scenario: I was working on a development branch and made several local changes. Before I could finish the fe…
🌐
Medium
medium.com › @sivaraaj › how-to-undo-the-most-recent-local-commits-in-git-7892fd717964
How to Undo the Most Recent Local Commits in Git ? | by Sivaraj Ramasamy | Medium
February 18, 2024 - $ git status On branch main Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: file1.txt modified: file2.txt $ git reset HEAD Unstaged changes after reset: M file1.txt M file2.txt $ git status On branch main 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: file1.txt modified: file2.txt · If you want to keep the changes but undo the last commit, use:
🌐
LabEx
labex.io › tutorials › git-how-to-undo-git-commit-but-keep-changes-392512
How to Undo Git Commit But Keep Changes | LabEx
In this comprehensive guide, you will learn how to undo a Git commit while keeping your changes intact.
🌐
Quora
quora.com › How-do-you-undo-a-git-commit-without-losing-your-files
How to undo a git commit without losing your files - Quora
There are several safe ways to undo a Git commit without losing your working files; choose based on whether the commit has been pushed and whether you want to keep changes staged or unstaged.
🌐
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
Hard Reset: git reset --hard HEAD~1 - Completely remove the last commit and discard changes. Soft Reset: git reset --soft HEAD~1 - Remove the last commit but keep changes in the working directory.
🌐
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
The --soft flag preserves your changes as staged modifications, so you can adjust them and recommit. If you'd prefer to unstage the changes but keep them in your working directory, omit the flag (the default is --mixed):
Published   5 days ago
🌐
Git
git-scm.com › book › en › v2 › Git-Basics-Undoing-Things
Git - Undoing Things
You end up with a single commit — the second commit replaces the results of the first. The next two sections demonstrate how to work with your staging area and working directory changes. The nice part is that the command you use to determine the state of those two areas also reminds you how to undo changes to them.
🌐
Reddit
reddit.com › r/opensource › 4 ways to undo a git commit - amend vs reset
r/opensource on Reddit: 4 Ways to Undo a Git Commit - Amend vs Reset
January 12, 2023 -

Maintainers will be more than happy if you keep the git history clean :D

I'm quite sure all of these happened to you at least once:

  • You committed a change with the wrong message (typo, wrong tense, etc.)

  • You committed a change with the wrong files (something missing, too many files, etc.)

  • You committed a change too early (feature isn't complete yet)

  • You committed a change you didn't want to (wrong code, just needs to be deleted)

Sure, you can just add a new commit, but in the long run, this will mess up your git history (unless you're closing PRs with squash).

If you want to keep your history clean and make your mistake disappear, let me show you 4 different ways to undo a commit. They're similar but not exactly the same, so you can apply the best one for your situation.

Bonus content: I'll also show you how to restore hard-deleted changes. What does that mean? I'll get into that later.

As usual, there's a live demo waiting for you on my YouTube channel where I show you all the content of this article plus some extra words and scenarios. If you're just here for the commands, you can skip the video and go straight to the article.

--> Click here for the video <--

Table of Contents

    1. Amend no-edit

  • 2. Amend & Change Message

  • 3. Reset (soft)

  • 4. Reset (hard)

  • Bonus: Restore Hard Deleted Changes

1. Amend no-edit

Let's start with the easiest situation, you already did a commit but you forgot to add some files.

Instead of creating an extra commit on top, you can run git commit --amend --no-edit. As a result, the last commit will be updated with the new files.

git add .
git commit --amend --no-edit

No extra actions required, you're done!

2. Amend & Change Message

Similar situation to the previous one, but you also want to change the commit message.

git add .
git commit --amend

This will open your default editor and you can change the commit message. Once you're done, save and close the editor. The commit will be updated with the new message.

Actually, git add . is not required if all you wanted to do is to change the commit message. You can just run git commit --amend.

3. Reset (soft)

This is the case where you want to undo a commit, but you want to keep the changes so that you can make a new commit at a later time.

git reset is kind of a time travel, really powerful but also dangerous. The most common use case is probably to undo a commit but keep in mind that you can do much more.

The full command we need is git reset --soft HEAD~1.

  • --soft means that the changes will be kept.

  • HEAD means the current commit you're on.

  • HEAD~1 means the last commit, but you can also use HEAD~2 to undo the last 2 commits.

  • A shortcut for HEAD is @, so @~1 would be the same as HEAD~1.

After running this command, you'll see that the last commit is gone but the files still have your changes applied.

You can now keep working and whenever you're ready you can do a new commit.

4. Reset (hard)

This is the case where you want to undo a commit and you don't want to keep the changes.

If you want to delete the changes, you need to add the --hard flag while running git reset.

Warning: this will also delete any uncommitted changes you have.

The full command we need this time is git reset --hard HEAD~1 and it will delete the last commit and the changes. Forever. Or is it?

Bonus: Restore Hard Deleted Changes

If you run git reset --hard HEAD~1 and you're not happy with the result, you can still restore the changes.

As we've just seen, git reset is our time travel machine, but we need to tell it where to go. In this case we entirely removed a commit and there's no trace in the git history, so we cannot say HEAD~number anymore.

Luckily, git keeps a log of all the commits that have been removed. You can see this log by running git reflog.

You want to look at a log like this one:

1b2c3d4 HEAD@{0}: reset: moving to HEAD~1

This means that you were at commit 1b2c3d4 and you did a reset to the previous commit. You can now use this commit ID to restore the changes.

git reset --hard 1b2c3d4

What are we doing here? We're telling git to go back to the commit 1b2c3d4 and to get rid of all the changes we did (in this case, the change was deleting the commit). Undoing a delete operation actually means restoring the deleted commit.

Conclusion

I hope you found this article useful and learned something new. If you have any questions or suggestions, feel free to leave a comment below.

I know there are at least a dozen different ways of moving around in git. I selected these 4 for simplicity but if you want to expand and add more in the comment section, let's do it!

Let me recommend once more to also watch the live demo on YouTube and leave a like to support my work.

Thanks!

🌐
Graphite
graphite.com › guides › undo-git-commit
How to undo a git commit
This command moves the current HEAD back one commit but keeps the changes in your staging area, allowing you to modify and recommit if needed. If you want to completely remove the last commit and all changes associated with it: ... This will ...