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
Navigate to your repository on ... GUI with git gui · Choose "Amend last commit". You will see your last commit message, the files you staged and the files you didn't. Now change things to how you want them to look and click Commit. ... Save this answer. ... Show activity on this post. If you want to revert the last commit but still want to keep the changes ... 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 to un-commit last un-pushed git commit without losing the changes - Stack Overflow
Commit hash of the last commit you want to keep. ... Save this answer. ... Show activity on this post. With me mostly it happens when I push changes to the wrong branch and realize later. And following works in most of the time. git revert commit-hash git push git checkout my-other-branch git ... More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
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   4 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.
🌐
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.
Find elsewhere
🌐
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~.
🌐
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 ...
🌐
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
1 month ago - 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 ...
🌐
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.
🌐
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.
🌐
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...
Top answer
1 of 15
103

Think we have code.txt file. We make some changes on it and commit. We can undo this commit in three ways, but first you should know what is the staged file... An staged file is a file that ready to commit and if you run git status this file will be shown with green color and if this is not staged for commit will be shown with red color:

It means if you commit your change, your changes on this file is not saved. You can add this file in your stage with git add code.txt and then commit your change:

Undo last commit:

  1. Now if we want to just undo commit without any other changes, we can use

    git reset --soft HEAD^

  2. If we want to undo commit and its changes (THIS IS DANGEROUS, because your change will lost), we can use

    git reset --hard HEAD^

  3. And if we want to undo commit and remove changes from stage, we can use

    git reset --mixed HEAD^ or in a short form git reset HEAD^

2 of 15
91

Usually, you want to undo a commit because you made a mistake and you want to fix it - essentially what the OP did when he asked the question. Really, you actually want to redo a commit.

Most of the answers here focus on the command line. While the command line is the best way to use Git when you're comfortable with it, its probably a bit alien to those coming from other version control systems to Git.

Here's how to do it using a GUI. If you have Git installed, you already have everything you need to follow these instructions.

NOTE: I will assume here that you realised the commit was wrong before you pushed it. If you don't know what pushing means, then you probably haven't pushed. Carry on with the instructions. If you have pushed the faulty commit, the least risky way is just to follow up the faulty commit with a new commit that fixes things, the way you would do it in a version control system that does not allow you to rewrite history.

That said, here's how to fix your most recent fault commit using a GUI:

  1. Navigate to your repository on the command line and start the GUI with git gui
  2. Choose "Amend last commit". You will see your last commit message, the files you staged and the files you didn't.
  3. Now change things to how you want them to look and click 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…
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