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
If you'd prefer to unstage the changes but keep them in your working directory, omit the flag (the default is --mixed): ... In case you're using the Tower Git client, you can simply hit CMD+Z (or CTRL+Z on Windows) to undo the last commit:
Published   5 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

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
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
I have 3 commits that is not pushed. I need to change the 1st commit. What is the best way to do it?
Start an interactive rebase. git rebase -i HEAD~3 In the editor that opens, change the word pick to edit on the commit you want to change. Save and exit. The rebase will pause at that commit, so you can delete the file. git rm --cached Amend the commit. git commit --amend --no-edit Continue the rebase. git rebase --continue Caveat emptor. More on reddit.com
🌐 r/git
22
25
August 26, 2024
How do I undo git add .? It seems I've added the entire main folder along with the node modules which I shouldn't have. I'm new to git
I highly – and I cannot stress this bold enough – recommend to actually read and understand all of the output git provides. Because in this picture it clearly says: No commits yet Untracked files: […] nothing added to commit but untracked files present "nothing added" means you did not do git add . (or any variant thereof), thus no need to undo it. You just initialized your repo, but nothing more. If the repo is in the wrong folder, you can just delete the .git subdirectory (rm -rf .git) and initialize it again in the correct folder. I also highly recommend to never use git add ., this will only lead to problems. Use git add for more controlled adding, or use git add -u to re-add only the modified tracked files. edit: In the future when you do need to undo an add, just read the output of git status – it will tell you the exact commands. More on reddit.com
🌐 r/git
23
0
June 8, 2024
🌐
GitLab
docs.gitlab.com › topics › git › undo
Revert and undo changes | GitLab Docs
Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: <file> no changes added to commit (use "git add" and/or "git commit -a") ... You can undo local changes that are already staged. In the following example, a file was added to the staging, but not committed:
🌐
TheServerSide
theserverside.com › tutorial › How-to-git-revert-a-commit-A-simple-undo-changes-example
How to revert a Git commit: A simple example | TheServerSide
The syntax to revert a Git commit and undo unwanted changes is simple. All developers need to do is issue the git revert command and provide the ID of the commit to undo: git@commit /c/revert example/ $ git revert 4945db2
🌐
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 - This undoes the last commit but keeps all changes in the staging area, so they’re ready for a new commit. For example, if you’re working on a billing module and realize you missed adding a necessary test file before committing, you don’t want to lose your changes or unstage them; you just want to modify the commit. ... Your changes are now back in the staging area. Add the missing test file: touch billing_test.js git add billing_test.js git commit -m "Add billing module with tests"
🌐
DataCamp
datacamp.com › blog › git-undo-last-commit
Git Undo Last Commit: Step-by-Step Guide for Beginners | DataCamp
June 23, 2025 - In Git, HEAD points to the latest commit in your project’s history. When you use the git reset command, you’re telling Git to move that pointer to a different commit. Let’s break down how to use git reset to undo the last commit in various scenarios.
Find elsewhere
🌐
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.
🌐
Git
git-scm.com › book › en › v2 › Git-Basics-Undoing-Things
2.4 Git Basics - Undoing Things
The nice part is that the command you use to determine the state of those two areas also reminds you how to undo changes to them. For example, let’s say you’ve changed two files and want to commit them as two separate changes, but you accidentally type git add * and stage them both.
🌐
Atlassian
atlassian.com › git › tutorials › undoing changes › git revert
How to Revert a Commit in Git? | Atlassian Git Tutorial
This can be useful, for example, if you’re tracking down a bug and find that it was introduced by a single commit. Instead of manually going in, fixing it, and committing a new snapshot, you can use git revert to automatically do all of this for you. The git revert command is used for undoing changes to a repository's commit history.
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-undo-a-commit-in-git
Undo a Commit in Git - GeeksforGeeks
April 7, 2026 - Step 2: To restore everything or undo all the changes we have to reset the commit. ... One can clearly see last commit (i.e. second commit) is removed. If a commit is already pushed, use git revert to create a new commit that reverses the changes.
🌐
GitProtect.io
gitprotect.io › strona główna › how to undo a commit in git
How to Undo a Commit in Git - Blog | GitProtect.io
January 7, 2026 - From a code perspective, we are ... ordinary UNDO. There was a change – there is no change. We got rid of the unwanted code. However, from Git’s perspective, it looks a bit different. This command creates a new commit that reverses the changes, without modifying or removing existing commits, which makes it safe to use in shared repositories. ... We already know several ways to roll back changes, each has its own advantages and disadvantages. But these were only theoretical examples to learn any ...
🌐
Linode
linode.com › docs › guides › how-to-undo-git-commit
Undo a Git Commit: A Step-by-Step Guide | Linode Docs
July 8, 2022 - ... cbe82f1a6 (HEAD -> git-test) Third revision of file. Take 3 6f819a796 Second revision of file 705dfa037 Initial draft of file · Undo the changes using the git reset --mixed command.
🌐
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 - TIP: Add a number to the end to undo multiple commits. For example, to undo the last 2 commits (assuming both have not been pushed) run Git reset—soft HEAD~2
🌐
GitHub
github.blog › home › open source › how to undo (almost) anything with git
How to undo (almost) anything with Git - The GitHub Blog
July 23, 2024 - If the old commit is “matter”, the new commit is “anti-matter”—anything removed in the old commit will be added in the new commit and anything added in the old commit will be removed in the new commit. This is Git’s safest, most basic “undo” scenario, because it doesn’t alter history—so you can now git push the new “inverse” commit to undo your mistaken commit.
🌐
KodeKloud
kodekloud.com › blog › git-uncommit-last-commit
How to Uncommit Last commit in Git (5 Scenarios)
November 25, 2025 - Then you commit them using git commit -m "Added hello world". To see the history of your commits, run this command: ... Now you realize that you made a mistake in hello.py, and you want to undo your commit.
🌐
GitKraken
gitkraken.com › home › learn › problems & solutions › how to revert a git commit
Git Revert Commit | Solutions to Git Problems
February 5, 2024 - Learn how to use Git revert to undo changes introduced in a specified commit or group of commits. See examples of Git revert commit in the terminal, GitKraken Client, & GitLens.
🌐
Adam Johnson
adamj.eu › tech › 2022 › 11 › 02 › git-how-to-undo-commits
Git: How to undo commits - Adam Johnson
November 2, 2022 - For example, say you wanted to undo the last two commits to undo: $ git log --oneline 68ae947 (HEAD -> soil) Add fertilizer 37b7eb0 Add compost 7372df9 Loosen soil ...
🌐
Warp
warp.dev › terminus by warp › git › undoing git commits
How To Undo Your Last Git Commit(s) | Warp
November 30, 2023 - Explore ways to undo a commit, including git reset, git checkout, and git revert with git while preserving commit history.
🌐
Towards Data Science
towardsdatascience.com › home › latest › git undo : how to rewrite git history with confidence
Git UNDO : How to Rewrite Git History with Confidence | Towards Data Science
April 22, 2026 - So the very last step you did before was to git commit, which actually means two things — Git created a commit object, and moved main, the active branch. To undo this step, use the command git reset --soft HEAD~1.
🌐
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.