do not use --hard use --soft instead.

Thus if you want to remove your latest commit you'd do:

git reset --soft HEAD^

Answer from Alexander Oh on Stack Overflow
Discussions

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
how do I revert to a previous commit without changes for this scenario ?
I do not think revert does what you think it will do. revert doesn't "revert" back to a previous point in history; it makes a new commit, which "reverts" (undoes / specifically does the opposite of) the commits you've given it. If you have changes to files after the range you've given, you likely will have a merge conflict to fix. To go back in time, you normally would reset to that point; however, if those commits are already on a 'public' server, and you do not want to edit history; then using reverts is the best way. If you are happy with the difference, and are sure which you want to do. Now in this case, you have a dirty tree. You currently have changes that you have not commited. you need to commit them, or stash them, or discard them as the error suggests. git reset --hard commitEarlierNo will get you there directly, if you do not mind potentially changing "public" history on some external server. otherwise git reset --hard HEAD followed by your revert of commits. now if you want, you can still keep those changes with a commit (so long as you correctly change the end revert range reference) or stash them (git stash) and get them back after with a git stash pop be aware that you may have conflicts after that. More on reddit.com
🌐 r/git
6
3
August 4, 2022
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
version control - How do I undo the most recent local commits in Git? - Stack Overflow
To undo your local commit you use git reset . Also that tutorial is very helpful to show you how it works. Alternatively, you can use git revert : reverting should be used when you want to add another commit that rolls back the changes (but keeps them in the project history). ... Be extra careful when reverting merge commits. You may lose ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Git
git-scm.com › docs › git-reset
Git - git-reset Documentation
Then, later you decide that it might be better to have each logical chunk associated with its own commit. You can use git reset to rewind history without changing the contents of your local files, and then successively use git add -p to interactively select which hunks to include into each ...
🌐
DEV Community
dev.to › edriso › how-to-undo-a-git-commit-without-losing-history-3ifp
How to Undo a Git Commit Without Losing History - DEV Community
April 23, 2026 - Rule of thumb: If the commit is already pushed, use git revert. If it's only local, git reset is fine. # See your commits git log --oneline # Revert the one you don't want git revert <commit-hash> --no-edit · Simple, safe, and no drama.
🌐
LabEx
labex.io › tutorials › git-how-to-revert-a-git-commit-without-losing-changes-415168
How to revert a Git commit without losing changes | LabEx
The --no-commit (or -n) option is perfect for this. First, let's reset our repository to the state before our last revert, so we can try a different approach. We will use git reset for this.
🌐
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 - If you’ve already committed and want to undo it while keeping the changes staged, use git reset –soft HEAD~1. This helps you adjust or re-commit without losing work.
Find elsewhere
🌐
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
Answer (1 of 7): If you have just run [code ]git commit[/code] and want to undo commit itself, leaving the working tree untouched then do: [code ]git reset —soft HEAD~1[/code] You can then edit the files and re-commit. If the intention of the undo is to edit the commit message, just do: [code...
🌐
Deployn
deployn.de › blog › git › git reset hard explained: irreversibly deleting commits (caution!)
Git Reset Hard Explained: Irreversibly Deleting Commits (Caution!)
April 14, 2025 - Alternative approaches should be considered that may be less disruptive and better suited to the specific situation. The git revert command allows you to undo specific commits without losing commit history.
🌐
Reddit
reddit.com › r/git › how do i revert to a previous commit without changes for this scenario ?
r/git on Reddit: how do I revert to a previous commit without changes for this scenario ?
August 4, 2022 -

Hi guys,

I am stuck in another tricky situation.

When I discovered I have done certain commits that are wrong, then I did :

git reset --hard commitNo1

I went on to do the changes that I need to only to discover that the code is already mixed with some code-generation stuff. No choice, I then wanted to revert to a earlier version :

I did

git revert --no-commit commitEarlierNo..HEAD

following this suggestion :

https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit

and now I got this below message:

error: Your local changes to the following files would be overwritten by merge:

Please commit your changes or stash them before you merge
Aborting
fatal: revert failed

Please help me now what can i do by aborting all the changes and I just want to revert to that even earlier version, and I am ok to discard changes I have made so far after I did that reset Hard to that commitNo1

🌐
Reddit
reddit.com › r/git › is there a way to remove all local commits while keeping the changes?
r/git on Reddit: Is there a way to remove all local commits while keeping the changes?
February 17, 2022 -

I would like to organize my PR after my feature is done by removing all of my 'work in progress'-commits and creating new commits that group files to improve the readability of my PR.
I have tried using git reset, but that only unstages changes that have been added using git add . & all files that are already committed seem to be out of reach.

🌐
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 ...
🌐
Codidact
software.codidact.com › posts › 286550
Is it possible to undo a git reset? - Software Development
If not, is it possible to just commit new stuff (that includes that change), and then later re-insert that version in the version history from the backup, so that afterwards it looks as if the commit had never been deleted? ... If you've committed, then the commit is in the git repo regardless. All git reset does is change what commit the HEAD references.
🌐
Academind
academind.com › articles › git-restore-and-git-reset
Git Restore and Git Reset | Academind
February 14, 2023 - git reset --hard HEAD~1 will reset the project by one commit and also delete any changes added to the project since the last commit.
🌐
JupyterLab
coderefinery.github.io › git-intro › recovering
Undoing and recovering — Introduction to version control with Git
git reset --hard HASH will force a branch label to any other point. All other changes are lost (but it is possible to recover if you force reset by mistake).
🌐
Quora
levelupcoding.quora.com › How-to-undo-a-git-commit-without-losing-your-files
How to undo a git commit without losing your files - Level Up Coding! - Quora
May 13, 2022 - Answer: Why would you undo but still want those file versions? If you staged some additional file just for that commit, the same goes — don't undo something you don't want to be ignored. If you want to save such files, store them elsewhere, such as another folder or repository, before undoing. ...
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.
🌐
DEV Community
dev.to › morinoko › useful-git-commands-for-removing-accidentally-pushed-or-committed-files-2ld
Useful Git Commands For Removing Accidentally Pushed or Committed Files - DEV Community
July 23, 2019 - I find myself needing to reset commits often because I committed a file I didn't want to or forgot to add something to a commit. Every once in a while, I also need to remove the file from the remote repository (but not locally) because I accidentally pushed it. I always forget the commands, so here they are for everyone's reference :) You can use the following command to delete a file from your git repo, for example, if you accidentally pushed it up or if you just don’t want it there any anymore.