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.

🌐
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
Top answer
1 of 16
30141

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

4 Ways to Undo a Git Commit - Amend vs Reset
what about rebasing to edit, squash, fix up, drop, or reorder commits interactively? More on reddit.com
🌐 r/opensource
3
8
January 12, 2023
How do I "undo" part of a commit that's in the past, and other commits have followed?
So, there's a few good ways to resolve this, and what you're doing is probably a good way if it's working for you. Don't beat yourself up -- yeah, it's a pain, for sure, but it gives you a result you can be confident in. Frankly, your confidence in the result is what matters more than anything else. There are other approaches with trade-offs. Three off the top of my head would be: Using revert and just sticking that commit straight on, which leaves you with a "messy history" Using revert and rebase to stick a commit back further down the history, then squashing those commits together, and Using rebase to just amend the commit. None of these actually resolve your actual problem, but I'll get to that in another comment, probably later in the day because I should be working right now. Let's talk about the first method, given it kind of plays into the second one a bit; git revert, described in the man page . You start by cleaning your working tree, maybe you git stash your current changes or whatever. Then you run git revert --edit . --edit, short form -e, is the default, to be clear, but I'll throw it in here for completeness sake; what it does is leave you with your working directory and index set up, poised to commit the revert. So... Go in, edit out the changes you want to keep from the commit using your preferred method, and hit commit. If you're in a position where you can't alter history, which may have several motivating reasons, that's probably going to be your only clean method. Or spinning up a new branch, as you do. The second approach would be to make that reverting commit, then use git rebase ( man page ) to reorder your commits and (optionally) squash it in. This is good if you need every commit in your history to work, for whatever reason. There's an awful lot you can do with rebase, but let's focus on what you want this time, an interactive rebase. Bluntly, the best thing to do in this case would be to look up a tutorial and have a bit of a play around, but broadly, you would run something like git rebase --interactive ~1 after committing in the commit reverting the undesired changes. Here, refers to the commit that has those bad changes. ~1 is something you might want to go to the gitrevisions man page for, but for now the easiest way to think of it is the "1st generation parent" of . What this will do, because you've specified --interactive (short form -i) is give you a text file in $EDITOR, whatever that's defined to be, with a list of commits in it. They'll be listed like so: pick Commit project files, oh no pick Do more work real good (...) pick Fix committed project files So from oldest, starting at , through to the latest commit, the reverting commit you just made. When you save and close that file, Git will run through those lines like a list of instructions, doing each thing. So, if you leave it, it'll pick the first commit, then pick the next commit, and so on. If you reorder those, well, lets say you do: pick Commit project files, oh no pick Fix committed project files pick Do more work real good (...) Then the revert commit will exist directly after in the resulting history. If you were to go one step further: pick Commit project files, oh no squash Fix committed project files pick Do more work real good (...) Then the revert commit would be squashed into the previous commit -- in this case, . That means, effectively, that you will no longer have a history with those bad changes in them. By default, squash concatenates the two commit messages. You could use fixup instead, which is the same but ignores the second commit's message. fixup -c does a bit more again, but at that point I'd suggest just reading that bit in the man page. The third method is kind of another step on from the second method. So, let's say you haven't made your revert commit yet. Dun dun duuuun, you're not actually going to! Run git rebase --interactive ~1. You'll see that file again. Then, let's say instead of reordering anything, you do this instead: edit Commit project files, oh no pick Do more work real good (...) Then you save and quit $EDITOR. Git will run through that list until it hits edit, and then it'll dump you into a situation where you're in the middle of the interactive rebase . You'll have your index populated with the commit, ready for you to edit away at it until it's exactly as you want it to be (ergo, remove the bad changes). Then, you resume the rebase with git rebase --continue, and git will go back to that list of commits (really a "todo list" in the rebase documentation lingo, you can actually do a bunch of things with that list) and run through the rest of it. More on reddit.com
🌐 r/git
8
13
August 18, 2021
How do you revert your code back to your last git commit?
Don't forget that you can always make a copy of the whole git repo folder on your computer so you can restore if you screw something up. Then you can mess around carefree! More on reddit.com
🌐 r/learnprogramming
21
1
June 4, 2022
New to git, advice needed. Revert commit but keep changes
You can either reset, as u/large_crimson_canine suggested, or you can make your new changes, git add them and then amend the commit you already made with: git --amend or, to retain the previous commit message: git --amend --no-edit More on reddit.com
🌐 r/git
17
0
November 18, 2023
🌐
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   1 day ago
🌐
GitLab
docs.gitlab.com › topics › git › undo
Revert and undo changes | GitLab Docs
Changes not staged for commit: ... 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:...
🌐
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.
🌐
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
🌐
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"
🌐
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.
🌐
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
🌐
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.
🌐
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.
🌐
Relia Software
reliasoftware.com › blog › git-undo-commit
How to Undo a Commit in Git? Code Examples Included | Relia Software
April 18, 2025 - Commands for Git undo commit are git restore, git commit –amend, git revert, git reset, and git rebase. Learn how to undo local commit, last commit, rewrite history,...
🌐
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 - Thus, I want to show you how you can undo changes in your repositories, and which git commands you need to use in your command line! Depending on the situation, undoing a commit can either preserve or rewrite history. We must therefore consider whether the change exists only locally or has already been pushed to a shared repository, as rewriting public history can be risky.
🌐
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.
🌐
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.
🌐
Atlassian
atlassian.com › git › tutorials › undoing changes › git revert
How to Revert a Commit in Git? | Atlassian Git Tutorial
For example, if you wanted to undo an old commit with git reset, you would have to remove all of the commits that occurred after the target commit, remove it, then re-commit all of the subsequent commits.
🌐
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 - 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.
🌐
Redirect
sethrobertson.github.io › GitFixUm
On undoing, fixing, or removing commits in git
If you see this message, your browser is broken. Please click here
🌐
Reddit
reddit.com › r/opensource › 4 ways to undo a git commit - amend vs reset
r/opensource on Reddit: 4 Ways to Undo a Git Commit - Amend vs Reset
January 12, 2023 -

Maintainers will be more than happy if you keep the git history clean :D

I'm quite sure all of these happened to you at least once:

  • You committed a change with the wrong message (typo, wrong tense, etc.)

  • You committed a change with the wrong files (something missing, too many files, etc.)

  • You committed a change too early (feature isn't complete yet)

  • You committed a change you didn't want to (wrong code, just needs to be deleted)

Sure, you can just add a new commit, but in the long run, this will mess up your git history (unless you're closing PRs with squash).

If you want to keep your history clean and make your mistake disappear, let me show you 4 different ways to undo a commit. They're similar but not exactly the same, so you can apply the best one for your situation.

Bonus content: I'll also show you how to restore hard-deleted changes. What does that mean? I'll get into that later.

As usual, there's a live demo waiting for you on my YouTube channel where I show you all the content of this article plus some extra words and scenarios. If you're just here for the commands, you can skip the video and go straight to the article.

--> Click here for the video <--

Table of Contents

    1. Amend no-edit

  • 2. Amend & Change Message

  • 3. Reset (soft)

  • 4. Reset (hard)

  • Bonus: Restore Hard Deleted Changes

1. Amend no-edit

Let's start with the easiest situation, you already did a commit but you forgot to add some files.

Instead of creating an extra commit on top, you can run git commit --amend --no-edit. As a result, the last commit will be updated with the new files.

git add .
git commit --amend --no-edit

No extra actions required, you're done!

2. Amend & Change Message

Similar situation to the previous one, but you also want to change the commit message.

git add .
git commit --amend

This will open your default editor and you can change the commit message. Once you're done, save and close the editor. The commit will be updated with the new message.

Actually, git add . is not required if all you wanted to do is to change the commit message. You can just run git commit --amend.

3. Reset (soft)

This is the case where you want to undo a commit, but you want to keep the changes so that you can make a new commit at a later time.

git reset is kind of a time travel, really powerful but also dangerous. The most common use case is probably to undo a commit but keep in mind that you can do much more.

The full command we need is git reset --soft HEAD~1.

  • --soft means that the changes will be kept.

  • HEAD means the current commit you're on.

  • HEAD~1 means the last commit, but you can also use HEAD~2 to undo the last 2 commits.

  • A shortcut for HEAD is @, so @~1 would be the same as HEAD~1.

After running this command, you'll see that the last commit is gone but the files still have your changes applied.

You can now keep working and whenever you're ready you can do a new commit.

4. Reset (hard)

This is the case where you want to undo a commit and you don't want to keep the changes.

If you want to delete the changes, you need to add the --hard flag while running git reset.

Warning: this will also delete any uncommitted changes you have.

The full command we need this time is git reset --hard HEAD~1 and it will delete the last commit and the changes. Forever. Or is it?

Bonus: Restore Hard Deleted Changes

If you run git reset --hard HEAD~1 and you're not happy with the result, you can still restore the changes.

As we've just seen, git reset is our time travel machine, but we need to tell it where to go. In this case we entirely removed a commit and there's no trace in the git history, so we cannot say HEAD~number anymore.

Luckily, git keeps a log of all the commits that have been removed. You can see this log by running git reflog.

You want to look at a log like this one:

1b2c3d4 HEAD@{0}: reset: moving to HEAD~1

This means that you were at commit 1b2c3d4 and you did a reset to the previous commit. You can now use this commit ID to restore the changes.

git reset --hard 1b2c3d4

What are we doing here? We're telling git to go back to the commit 1b2c3d4 and to get rid of all the changes we did (in this case, the change was deleting the commit). Undoing a delete operation actually means restoring the deleted commit.

Conclusion

I hope you found this article useful and learned something new. If you have any questions or suggestions, feel free to leave a comment below.

I know there are at least a dozen different ways of moving around in git. I selected these 4 for simplicity but if you want to expand and add more in the comment section, let's do it!

Let me recommend once more to also watch the live demo on YouTube and leave a like to support my work.

Thanks!

🌐
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.