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

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
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
Is there a way to remove all local commits while keeping the changes?
You can git reset --soft to the last commit you want to keep: https://git-scm.com/docs/git-reset More on reddit.com
🌐 r/git
27
5
February 17, 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 ...
🌐
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~.
🌐
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!

🌐
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 - git reset --mixed HEAD~1 (or git reset HEAD~1): This is the default behavior of git reset. It moves the HEAD pointer back one commit and unstages the changes but keeps them in your working directory. git reset --hard HEAD~1: This command discards ...
Find elsewhere
🌐
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 - This undoes the last commit but keeps all changes in the staging area, so they’re ready for a new commit. For example, if you’re working on a billing module and realize you missed adding a necessary test file before committing, you don’t ...
🌐
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 - To discard the last commit and its changes permanently, use git reset --hard HEAD~1 (with extreme caution). For preserving history with a new commit, use git revert. Use git rebase -i only for advanced history modifications with clear understanding ...
🌐
LabEx
labex.io › tutorials › git-how-to-undo-git-commit-but-keep-changes-392512
How to Undo Git Commit But Keep Changes | LabEx
To completely discard the most recent commit and remove it from your commit history, you can use the git reset command with the --hard option: ... Move the current branch pointer back one commit, effectively undoing the most recent commit.
🌐
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
In case you're using the Tower Git client, you can simply hit CMD+Z (or CTRL+Z on Windows) to undo the last commit: You can use this familiar keyboard shortcut to undo many other actions, such as a failed merge or a deleted branch!
Published   5 days ago
🌐
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 ...
🌐
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…
🌐
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.
🌐
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
To keep file changes but remove ... or git reset --mixed (keeps unstaged). Avoid --hard unless you want to discard working-tree changes. ... These commands preserve your file contents except when using --hard; choose the option that matches whether the commit has been shared. ... That's it, your commit changes will be in your working directory, whereas the LAST commit will ...
🌐
DEV Community
dev.to › andyrewlee › how-to-undo-last-commit-and-keep-changes-1eh2
How to Undo Last Commit and Keep Changes - DEV Community
April 30, 2020 - To undo the last commit but keep the changes, run the following command: git reset --soft HEAD~1... Tagged with git, beginners, tutorial.
🌐
devconnected
devconnected.com › home › software engineering › how to undo last git commit
How To Undo Last Git Commit – devconnected
December 23, 2019 - In order to undo the last Git commit, keep changes in the working directory but NOT in the index, you have to use the “git reset” command with the “–mixed” option.
🌐
Claude Code Docs
code.claude.com › use claude code › best practices for claude code
Best practices for Claude Code - Claude Code Docs
3 days ago - What files need to change? What's the session flow? Create a plan. Press Ctrl+G to open the plan in your text editor for direct editing before Claude proceeds. ... Switch out of plan mode and let Claude code, verifying against its plan. ... implement the OAuth flow from your plan. write tests for the callback handler, run the test suite and fix any failures. ... Ask Claude to commit with a descriptive message and create a PR.
🌐
Mendix
marketplace.mendix.com › link › studiopro
Mendix Studio pro
5 days ago - The Mendix Marketplace is at the heart of the Mendix developer community, which is made up of over 175,000 developers. By providing everything from simple widgets to templatized solutions, the Marketplace has the resources you need to build innovative solutions.
🌐
Howtoundomylastcommit
howtoundomylastcommit.com
How to Undo My Last Commit
Quick reminder for when you need to undo that last git commit · Undoes the commit but keeps your changes staged and ready to commit again.
🌐
Codingem
codingem.com › home › git how to undo commit: a step-by-step guide (in 5+ cases)
Git How to Undo Commit: A Step-by-Step Guide (in 5+ Cases)
July 10, 2025 - Depending on what you’re trying ... use the git reset command to undo the last commit or commits: ... Let’s take a closer look at each command. To undo the most recent commit that you haven’t pushed run the following command: ... This ...