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.

🌐
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
What if you want to undo the effects of a commit that's not the most recent one — without touching any commits that came after it? That's what git revert is for.
Published   6 days ago
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.

Discussions

git revert --no-commit without staging - Stack Overflow
But after this command, the reverted files are in the staged area. I can unstage them by using the command git reset HEAD. Is there a direct way to revert a commit without committing and staging? More on stackoverflow.com
🌐 stackoverflow.com
Can you git revert a commit without reverting recent commits?
Yes, you can revert a single commit anywhere in the history. Depending on the changes made since that commit, you may need to resolve conflicts. https://git-scm.com/docs/git-revert More on reddit.com
🌐 r/git
5
12
March 29, 2023
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
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
🌐
Git
git-scm.com › docs › git-revert
Git - git-revert Documentation
Note: git revert is used to record some new commits to reverse the effect of some earlier commits (often only a faulty one). If you want to throw away all uncommitted changes in your working directory, you should see git-reset[1], particularly the --hard option.
🌐
freeCodeCamp
freecodecamp.org › news › git-revert-commit-how-to-undo-the-last-commit
Git Revert Commit – How to Undo the Last Commit
August 31, 2021 - If you want to reset to the last commit and also remove all unstaged changes, you can use the --hard option: ... This will undo the latest commit, but also any uncommitted changes.
🌐
GitKraken
gitkraken.com › home › learn › problems & solutions › how to revert a git commit
Git Revert Commit | Solutions to Git Problems
February 5, 2024 - If you need to make revisions to a commit that is not your last commit, the best solution is to create a new commit by reverting the old commit. As a Git best practice, you should avoid doing anything that will require you to force push — ...
🌐
KodeKloud
kodekloud.com › blog › git-uncommit-last-commit
How to Uncommit Last commit in Git (5 Scenarios)
November 25, 2025 - For commits already pushed to a remote repository, use git revert <commit_hash> --no-edit to safely undo changes without rewriting history.
Find elsewhere
🌐
Atlassian
atlassian.com › git › tutorials › undoing changes › git revert
How to Revert a Commit in Git? | Atlassian Git Tutorial
Git revert expects a commit ref was passed in and will not execute without one. Here we have passed in the HEAD ref. This will revert the latest commit. This is the same behavior as if we reverted to commit 3602d8815dbfa78cd37cd4d189552764b5e96c58.
🌐
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 › @eduardosilva_94960 › git-tips-understanding-the-power-of-revert-with-no-commit-77db3cce4ca
Git Tips — Understanding the Power of revert with — no-commit | by Eduardo Silva | Medium
July 29, 2023 - Here’s how you can do it with git revert --no-commit: $ git log --oneline a1b2c3d (HEAD) Fix: Issue with login feature e4f5g6h Add: New homepage design i7j8k9l Implement: User authentication · To revert the “Fix: Issue with login feature” ...
🌐
Atlassian
atlassian.com › git › tutorials › undoing changes
Undoing Changes in Git | Atlassian Git Tutorial
December 15, 2025 - Let's assume we are back to our original commit history example. The history that includes the 872fa7e commit. This time let's try a revert 'undo'. If we execute git revert HEAD, Git will create a new commit with the inverse of the last commit.
🌐
Ben Holmes
bholmes.dev › blog › how-to-revert-your-git-commits-without-panicking
How to revert your git commits without panicking - Ben Holmes
Worth warning that this will open ... type ":wq" to confirm the reverts. You can also add the --no-commit flag to stage each revert without actually creating a revert commit....
🌐
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.
🌐
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/learnprogramming › how do you revert your code back to your last git commit?
r/learnprogramming on Reddit: How do you revert your code back to your last git commit?
June 4, 2022 -

Sorry for the noob question but all the google answers are spread out over 10 years and totally different answers. I can't find a common consensus on this and don't want to screw anything up.

I just need to go back to my last commit. I committed and everything was fine but recent code changes broke the site and I need to go back to when things were working.

It says in the terminal:

(use "git restore <file>..." to discard changes in working directory)

This is what I'm looking for right? If I do "git restore <file>" it will discard all my changes and go back to what the file was like before I made the breaking changes?

Also, if so, I have a lot of files to change, if I just do "git restore" without a specified file will it restore ALL the files that have been modified so I don't have to go one by one?

🌐
Linuxize
linuxize.com › home › git › how to revert a commit in git
How to Revert a Commit in Git | Linuxize
May 23, 2026 - The --no-commit option stages all the reverted changes without creating individual revert commits, allowing you to commit them as a single revert: ... Without --no-commit, Git will create a separate revert commit for each commit in the range.
🌐
PhoenixNAP
phoenixnap.com › home › kb › sysadmin › git revert commit: how to undo last commit
Git Revert Commit: How to Undo Last Commit
February 4, 2026 - The --no-edit option amends the commit without changing the commit message. Note: If you need to restore a missing repository, refer to our guide How to Restore a Git Repository. ... You now know how to use the revert and reset commands to undo ...
🌐
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 - # See your commits git log --oneline # Revert the one you don't want git revert <commit-hash> --no-edit