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

version control - How do I undo the most recent local commits in Git? - Stack Overflow
FYI: This removes all my files and I lost changes. 2020-05-15T10:03:11.34Z+00:00 ... UPD: However, I've restored it using reflog. But the receipt did not work for the initial commit. 2020-05-15T11:25:56.753Z+00:00 ... Use git rm --cached to keep the files in the filesystem and only delete them from the git index! 2020-05-19T10:00:55.163Z+00:00 ... " git --amend --no-edit " will commit your last ... More on stackoverflow.com
🌐 stackoverflow.com
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
How do I undo the most recent local commits in Git? [Tutorial]
git reset**:** This allows you to move the HEAD to a specific state, effectively undoing commits while keeping your changes staged or unstaged. Just remember to use it carefully, as it can alter your commit history. git restore**:** this provides a simpler way to undo changes in the working directory and staging area introduced by the most recent commit. It's a handy tool for reverting changes quickly. git revert**:** If you've already pushed your commits to a remote repository and want to undo them without altering history, git revert is a safe option. It creates a new commit that undoes the changes made in a specified commit. git checkout**:** While primarily used to switch branches, git checkout can also help you undo local commits by moving the HEAD pointer to a previous commit. It's useful for discarding the most recent commit. git commit --amend**:** If you only need to modify the commit message of your most recent commit, this command comes in handy. It allows you to edit the commit message without creating a new commit. Or the solution I've adopted a while ago and never looked back, lol, is cmd+z from Tower , their undo command is pure magic. More on reddit.com
🌐 r/git
3
0
May 1, 2024
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
🌐
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!

🌐
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   6 days ago
🌐
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.
🌐
Graphite
graphite.com › guides › git-undo-last-commit
Git undo last commit - Graphite
... 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~.
Find elsewhere
🌐
GitLab
docs.gitlab.com › topics › git › undo
Revert and undo changes | GitLab Docs
Undo your last commit and put everything back in the staging area: ... Edit a file. ... If a file was changed in a commit, 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.
🌐
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 log --oneline a1b2c3d (HEAD ... of the last commit but undo its changes, use: ... This method creates a new commit that reverses the effects of the specified commit....
🌐
LabEx
labex.io › tutorials › git-how-to-undo-git-commit-but-keep-changes-392512
How to Undo Git Commit But Keep Changes | LabEx
Remove any untracked files (files that are not part of your Git repository) from your working directory. After running this command, your working directory will be restored to the state of the last commit, and any uncommitted changes will be ...
🌐
KodeKloud
kodekloud.com › blog › git-uncommit-last-commit
How to Uncommit Last commit in Git (5 Scenarios)
November 25, 2025 - To undo the act of committing and also unstage your changes but keep your files intact, use git reset HEAD. To undo everything, including discarding your changes and resetting your files to the previous commit, use git reset --hard HEAD.
🌐
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 ...
🌐
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
Behavior: rewrites history; changes propagate to subsequent commits. Use only for local or carefully coordinated branches. ... If the commit has been pushed to a remote used by others, prefer git revert to avoid forcing others to rebase. If you only want to adjust commit message or include additional staged files, use git commit --amend. To keep ...
🌐
DataCamp
datacamp.com › tutorial › git-revert-last-commit
Git Revert Last Commit: How to Safely Undo a Change in Git | DataCamp
July 8, 2025 - It adds a new commit: As I had mentioned already, rather than wiping anything away, git revert creates a fresh commit that undoes the specific changes from the one you're reverting. This keeps your project history consistent.
🌐
Jessica Temporal
jtemporal.com › undoing-the-last-commit-and-reusing-the-message
Undoing the last commit and keeping the changes for a next commit | Jessica Temporal
June 20, 2026 - Given this scenario, the first step is to use the command git reset. Maybe you don’t know that there is a flag that while undoing a commit with git reset, it allows you to keep the commit changes on staging and the commit message stored in ...
🌐
GitHub
gist.github.com › wilsonsilva › c6f870e6423a1c0076224d1f1e468dbb
Undo last commit but keep changes - Gist - GitHub
How can I delete a specific commit and also keep changes? Copy link · Copy Markdown · Thank you · Copy link · Copy Markdown · @Arman-Ghazaryan something like · git revert --no-commit abc123 · See git revert · Copy link · Copy Markdown · Awesome! Copy link ·
🌐
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.
Top answer
1 of 16
30140

Undo a commit & redo

$ git commit -m "Something terribly misguided" # (0: Your Accident)
$ git reset HEAD~                              # (1)
# === If you just want to undo the commit, stop here! ===
[ edit files as necessary ]                    # (2)
$ git add .                                    # (3)
$ git commit -c ORIG_HEAD                      # (4)
  1. git reset is the command responsible for the undo. It will undo your last commit while leaving your working tree (the state of your files on disk) untouched. You'll need to add them again before you can commit them again.
  2. Make corrections to working tree files.
  3. git add anything that you want to include in your new commit.
  4. Commit the changes, reusing the old commit message. reset copied the old head to .git/ORIG_HEAD; commit with -c ORIG_HEAD will open an editor, which initially contains the log message from the old commit and allows you to edit it. If you do not need to edit the message, you could use the -C option.

Alternatively, to edit the previous commit (or just its commit message), commit --amend will add changes within the current index to the previous commit.

To remove (not revert) a commit that has been pushed to the server, rewriting history with git push origin main --force[-with-lease] is necessary. It's almost always a bad idea to use --force; prefer --force-with-lease instead, and as noted in the git manual:

You should understand the implications of rewriting history if you amend a commit that has already been published.


Further Reading

You can use git reflog to determine the SHA-1 for the commit to which you wish to revert. Once you have this value, use the sequence of commands as explained above.


HEAD~ is the same as HEAD~1. The article What is the HEAD in git? is helpful if you want to uncommit multiple commits.

2 of 16
13009

Undoing a commit is a little scary if you don't know how it works. But it's actually amazingly easy if you do understand. I'll show you the 4 different ways you can undo a commit.

Say you have this, where C is your HEAD and (F) is the state of your files.

   (F)
A-B-C
    ↑
  master

Option 1: git reset --hard

You want to destroy commit C and also throw away any uncommitted changes. You do this:

git reset --hard HEAD~1

The result is:

 (F)
A-B
  ↑
master

Now B is the HEAD. Because you used --hard, your files are reset to their state at commit B.

Option 2: git reset

Maybe commit C wasn't a disaster, but just a bit off. You want to undo the commit but keep your changes for a bit of editing before you do a better commit. Starting again from here, with C as your HEAD:

   (F)
A-B-C
    ↑
  master

Do this, leaving off the --hard:

git reset HEAD~1

In this case the result is:

   (F)
A-B-C
  ↑
master

In both cases, HEAD is just a pointer to the latest commit. When you do a git reset HEAD~1, you tell Git to move the HEAD pointer back one commit. But (unless you use --hard) you leave your files as they were. So now git status shows the changes you had checked into C. You haven't lost a thing!

Option 3: git reset --soft

For the lightest touch, you can even undo your commit but leave your files and your index:

git reset --soft HEAD~1

This not only leaves your files alone, it even leaves your index alone. When you do git status, you'll see that the same files are in the index as before. In fact, right after this command, you could do git commit and you'd be redoing the same commit you just had.

Option 4: you did git reset --hard and need to get that code back

One more thing: Suppose you destroy a commit as in the first example, but then discover you needed it after all? Tough luck, right?

Nope, there's still a way to get it back. Type this

git reflog

and you'll see a list of (partial) commit SHAs (that is, hashes) that you've moved around in. Find the commit you destroyed, and do this:

git checkout -b someNewBranchName shaYouDestroyed

You've now resurrected that commit. Commits don't actually get destroyed in Git for some 90 days, so you can usually go back and rescue one you didn't mean to get rid of.

🌐
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…
🌐
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...
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-revert-to-last-commit
How to Revert to Last Commit? - Git
June 6, 2024 - This article will guide you through various approaches to revert to the last commit, detailing each step to ensure you can effectively manage your Git repository. Reverting to the last commit means discarding the changes made after the last commit and bringing your working directory back to the state it was in at that commit.