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.

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.

🌐
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
Undo the last commit with git reset, revert an older commit with git revert, or remove a commit from history with interactive rebase. All scenarios, step by step.
Published   6 days ago
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
Delete last Git commit from local and remote repositories.
I want to basically start over ... from the local repository. Ideally, then I would be back to where I started originally, as if I had just created the branch. I have not yet run any commands. ... We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads. ... You can use the git revert command followed by the commit number. This will cause git to create a commit with the changes to undo the last commits, which ... More on experts-exchange.com
🌐 experts-exchange.com
August 1, 2022
How to Undo the Most Recent Local Commits in Git?
Tbh this whole article reads like it was written by AI. Especially since all of the "examples" are just the commands without explanation or an example output. I know Git pretty well, but even I had trouble following your article, so I don't think it'll be very helpful to beginners. More on reddit.com
🌐 r/git
2
0
February 21, 2024
revert to previous commit on github?
As a general rule, once you push your repository to a remote repository, your commits should be considered permanent. By resetting your repository to an earlier state, you made it impossible to push to the remote repository. You have a couple options: git push -f will override that rule and allow you to push. I don't recommend this option unless you're the only user of that remote repository. If other people have been pulling from that repository, you will very likely mess them up. You also might not have the necessary permissions to do this if this is someone else's repository. Go back to where you before you executed git reset --hard and do it right. You could execute these steps: commands: git branch foo (makes a placeholder to where you are now) git pull (get your local repository back in sync with the remote) git revert git push What git revert does is construct a whole new commit that un-does the one you want to get rid of. The old one remains in the git history and then there's a new commit that reverses it. There's no way to really get rid of the old one since it's there in the remote repository and in the repositories of anybody who's pulled from it. If what you want to do something more complicated than just reverting one commit, then there is where branch "foo" comes in: git branch foo (makes a placeholder to where you are now) git pull (get your local repository back in sync with the remote) git diff HEAD foo > /tmp/patch patch -p1 < /tmp/patch git commit -a git push If it's not obvious, what this does is construct a patch that will change all the files in your current branch to what they were in your "reverted" branch. Then you apply that patch and commit the changes. My notes on the subject: http://www.efalk.org/Docs/Git/merging.html Disclaimer: these are Unix commands. If you're on Windows, you're on your own here. More on reddit.com
🌐 r/git
3
0
January 17, 2025
🌐
Reddit
reddit.com › r/git › how to undo the most recent local commits in git?
r/git on Reddit: How to Undo the Most Recent Local Commits in Git?
February 21, 2024 - I did find: git reset HEAD@{the entry from the git reflog}; nice, but that is not undoing a recent commit; that is resetting the branch to a state with or without the commit.
🌐
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 -> main) Add new feature e4f5g6h Initial commit $ git reset --hard HEAD~1 HEAD is now at e4f5g6h Initial commit $ git status On branch main nothing to commit, working tree clean $ git log --oneline e4f5g6h (HEAD -> main) Initial commit · If you want to preserve the history of the last commit but undo its changes, use: ... This method creates a new commit that reverses the effects of the specified commit. It maintains the original commit in your history but adds a new “revert” commit to undo its 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
🌐
GitHub
gist.github.com › vishaltelangre › 5531806
Revert last local or remote commit (git) · GitHub
Now git log will show that our last commit has been removed. ... If you have already made your commits public, you will want to create a new commit which will "revert" the changes you made in your previous commit (current HEAD).
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-undo-the-most-recent-local-commits-in-git
How to Undo the Most Recent Local Commits in Git? - GeeksforGeeks
July 23, 2025 - This example undoes the last three commits, keeping the changes staged. The `git revert` command is a safer alternative, especially when working with a shared repository, as it creates a new commit that undoes the changes from the specified commit.
🌐
Sentry
sentry.io › sentry answers › git › undo the most recent local git commits
Undo the most recent local Git commits | Sentry
From this list, you can pick out the partial hash of the commit (e.g. 5c8f5a7) to restore and create a new branch for it: ... If you would like to preserve your repository’s history but return the files to a previous state, you can use git revert to create new commits that do the opposite of existing commits, i.e.
🌐
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 - Undoing commits in Git is simple with the right method. Use git reset for local changes and git revert for pushed commits. If something goes wrong, git reflog helps you recover lost work.
🌐
Experts Exchange
experts-exchange.com › questions › 29244073 › Delete-last-Git-commit-from-local-and-remote-repositories.html
Solved: Delete last Git commit from local and remote repositories. | Experts Exchange
August 1, 2022 - ... You can use the git revert command followed by the commit number. This will cause git to create a commit with the changes to undo the last commits, which is what I would recommend.
🌐
CloudBees
cloudbees.com › blog › git-undo-commit
How to Undo Changes in Git with Git Undo Commit
November 25, 2020 - Running this command will open ... edit the last commit message. Before running git command --amend, remember to first stage your new changes with git add. Close your text editor after typing in the new commit message.
🌐
Git Skills
smartgit.dev › git-how-to › undo-last-commit
How to Undo Last Git Commits | Git Skills
Undoing One Single Commit: To undo just the latest commit, simply invoke Local | Undo Last Commit.
🌐
DataCamp
datacamp.com › blog › git-undo-last-commit
Git Undo Last Commit: Step-by-Step Guide for Beginners | DataCamp
June 23, 2025 - Let’s say you made a few commits on a local branch and accidentally pushed them to the main branch. Oops. Now you want to undo one of those commits without messing up the shared history. That’s where git revert saves the day. Run git revert HEAD~1. This creates a new commit that undoes ...
🌐
Warp
warp.dev › terminus by warp › git › undoing git commits
How To Undo Your Last Git Commit(s) | Warp
November 30, 2023 - If you decide to use git reset or git checkout to overwrite commits, then you can use git push --force to overwrite the remote branch with your local branch. This will overwrite any commits that were made after your last ...
🌐
Linode
linode.com › docs › guides › how-to-undo-git-commit
Undo a Git Commit: A Step-by-Step Guide | Linode Docs
July 8, 2022 - To use Git to revert a commit, follow these steps. Verify the file history and contents to determine the change to revert. ... 34722a3fd (HEAD -> git-test) Third revision of file. Take 4 6f819a796 Second revision of file 705dfa037 Initial draft of file ... Apply git revert to the last commit.
🌐
JetBrains
jetbrains.com › help › idea › undo-changes.html
Undo changes in Git repository | IntelliJ IDEA Documentation
May 5, 2026 - In the Commit tool window (Alt+0) , select one or more files that you want to revert, and select Rollback from the context menu, or press Ctrl+Alt+Z. In the dialog that opens, check the list of files to be reverted.
🌐
Nobledesktop
blog.nobledesktop.com › learn › git › undo changes in git: git checkout, git revert, & git reset
Undo Changes in Git: checkout, revert, & reset
April 19, 2026 - For example, to undo the last 2 commits (assuming both have not been pushed) run Git reset—soft HEAD~2 · NOTE: Git reset—soft HEAD~ is the same as Git reset—soft HEAD^ which you may see in Git documentation.
🌐
Graphite
graphite.com › guides › git-undo-last-commit
Git undo last commit
Q: What's the difference between git reset --soft HEAD~ and git reset --hard HEAD~? A: git reset --soft HEAD~ undoes the last commit but leaves your changes in your working directory, so you can modify them and re-commit.