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.

🌐
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.
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
How to undo the last local commit in Git?
Recently I was making some changes to a local git repository and I committed some changes and files that I should not have committed. I did not run `git p… More on digitalocean.com
🌐 digitalocean.com
2
January 8, 2020
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
How do I undo the last commit without losing changes?
If you only want to undo changes for one file (not the whole commit), you can restore that file from the previous commit: ... Beta Was this translation helpful? Give feedback. ... Sign up for free to join this conversation on GitHub. Already have an account? More on github.com
🌐 github.com
3
2
🌐
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 - Use it if you want to retain the changes but revise them before committing. For example, suppose you have committed a change to file1.txt with the message “Add new feature”. You can use git reset --soft HEAD~1 to undo the commit and keep ...
🌐
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   20 hours ago
🌐
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.
Find elsewhere
🌐
Git
git-scm.com › book › en › v2 › Git-Basics-Undoing-Things
Git - Undoing Things
However, anything you lose that was never committed is likely never to be seen again. Git version 2.23.0 introduced a new command: git restore. It’s basically an alternative to git reset which we just covered. From Git version 2.23.0 onwards, Git will use git restore instead of git reset ...
🌐
Sentry
sentry.io › sentry answers › git › undo the most recent local git commits
Undo the most recent local Git commits | Sentry
February 15, 2023 - When calling git reset, you need to specify the commit to reset to. You can get the hash of the commit you want from git log, or you can specify an ancestor of HEAD, the current commit, using the tilde (~) suffix.
🌐
DigitalOcean
digitalocean.com › community › questions › how-to-undo-the-last-local-commit-in-git
How to undo the last local commit in Git? | DigitalOcean
January 8, 2020 - If you have not run git push so that the changes were only committed to your local Git repository. That means there are a few solutions. Let’s say that you made some changes and you committed the changes: ... After that, if you run git log you will see the history of everything that has been committed to a repository. To undo the last commit, you just need to run the following command:
🌐
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.

🌐
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 - Staged changes are committed to the local repository using git commit. git commit -a directly commits all modified files. ... Contains the committed changes. Changes can be undone using git reset < commit>.
🌐
DeployHQ
deployhq.com › git learning › faqs › how do i undo the most recent local commits in git?
How do I undo the most recent local commits in Git? - DeployHQ
How to use the git reset command to undo several local commits in your Git repository, with options for soft, mixed, and hard reset.
🌐
CloudBees
cloudbees.com › blog › git-undo-commit
How to Undo Changes in Git with Git Undo Commit
November 25, 2020 - 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. When you come back to your terminal, you'll see something like this: ... The three undo ...
🌐
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.
🌐
DEV Community
dev.to › isabelcmdcosta › how-to-undo-the-last-commit--31mg
Git Revert Pushed Commit: How to undo the last commit - DEV Community
February 20, 2018 - I use git reset HEAD~1 to undo the last commit from a local branch and keep the modifications, this is a way for easily fix the changes
🌐
Linode
linode.com › docs › guides › revert-last-git-commit
How to Revert the Last Commit in Git | Linode Docs
February 15, 2024 - It gives you full transparency to past commits and their reversions. In contrast, using reset discards commits even from the commit history, making them impossible to recover later. The git reset command can even do the same for local file changes.
🌐
JanBask Training
janbasktraining.com › community › devops › how-do-i-undo-the-most-recent-local-commits-in-git
How do I undo the most recent local commits in Git? | JanBask Training Community
March 18, 2025 - You can undo the last commit in Git using git reset. Use --soft to keep changes staged, --mixed to unstage them, or --hard to discard them completely.
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 206884545-How-to-undo-a-commit-in-a-local-branch-git
How to undo a commit in a local branch - git – IDEs Support (IntelliJ Platform) | JetBrains
January 30, 2013 - To undo local unpushed commits in Git you have to use the reset command. If you want to keep code changes, you need the "mixed" mode (which is the default mode).
🌐
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.
🌐
Atlassian
atlassian.com › git › tutorials › undoing changes › git revert
How to Revert a Commit in Git? | Atlassian Git Tutorial
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.