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
30139

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   4 days ago
Discussions

Undoing last commit
You should use git revert in this situation. It will undo the changes made by the faulty commit. Then push the newly created commit to remote. See https://git-scm.com/docs/git-revert for more info. More on reddit.com
🌐 r/git
12
6
January 7, 2021
Delete last Git commit from local and remote repositories.
Delete last Git commit from local and remote repositories. Need to delete the last (and only) commit from both local and remote repositories. I created a branch (call it dev-branch) and made a change to a file. Then I added, then committed the change locally. Then I pushed the commit to the remote repository. It has NOT been merged. I want to basically start over - remove ... More on experts-exchange.com
🌐 experts-exchange.com
August 1, 2022
How can I completely delete commits from both local and remote?
Yes. Your solution will work. git reset --hard [commit hash] will wipe everything after that commit. git push -f [origin branchname] Will make your remote like that too. Two precautions to take note of: This will clean out your directory. If you have any files in there that you want that are not part of previous commits you need to get them out. Using a git push -f without specifying origin branchname afterwards has the potential to force push commits on other branches or possibly overwrite your remote master branch if you are on a forked repository and you haven’t pulled in a bit. This can cause you headaches if you have pull requests out or other situations. It is always best to specify the branch name when using the force push. Additionally, take note of the command git reset --soft . This will allow you to revert your commit history in the same fashion as above while leaving the changes in your files as staged files. This is the solution if you don’t want your current commit history but you do want your current file changes. After this command, you can do a plain git reset to unstage these files or a git commit to make a new commit. More on reddit.com
🌐 r/git
3
5
May 11, 2020
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
People also ask

How to Undo, Revert, or Delete a Git Commit
To undo the last local commit (one that hasn't been pushed yet) while keeping your changes staged, run git reset --soft HEAD~1. To unstage the changes but keep the edits in your working directory, use git reset --mixed HEAD~1. To discard the changes entirely, use git reset --hard HEAD~1 — this permanently deletes the uncommitted work. To undo a specific older commit without altering history, use git revert , which creates a new commit that applies the reverse of the targeted commit's changes; this is the safest approach for shared branches. The --no-commit flag stages the reverting changes wit
🌐
git-tower.com
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 ...
What happens if I encounter conflicts while reverting a commit?
Resolve the conflicts manually, stage the changes, and then run `git revert --continue` to complete the revert operation.
🌐
blog.openreplay.com
blog.openreplay.com › openreplay blog › undoing git commits after push: safely revert changes on remote repositories
Undoing Git Commits After Push: Safely Revert Changes on Remote ...
🌐
GitHub
gist.github.com › CrookedNumber › 8964442
git: Removing the last commit · GitHub
... To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits.
🌐
Reddit
reddit.com › r/git › undoing last commit
Undoing last commit : r/git
January 7, 2021 - If you don't want those changes anymore at all, you can use git reset --hard HEAD~1. Beware this is a destructive action, you bad commit will disappear entirely. ... This is the tutorial git: how to undo last commit that helped me.
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-delete-last-commit-in-git
How to Delete Last Commit in Git? - GeeksforGeeks
July 23, 2025 - Deleting the last commit in Git is a simple process that can help fix mistakes or clean up your commit history. Just be careful when using commands like --hard or --force, as they can permanently remove your work.
Find elsewhere
🌐
GitHub
gist.github.com › cutiko › 0b1615c63504a940877541362cc51211
Git delete last commit · GitHub
... To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits.
🌐
freeCodeCamp
freecodecamp.org › news › git-remove-last-commit-how-to-undo-a-commit-in-git
Git Remove Last Commit – How to Undo a Commit in Git
September 21, 2022 - With the git log command, you can also check which commit you want to undo. Say that your latest commit has a commit hash of cc3bbf7, which is followed by (HEAD -> main, origin/main), and a commit message such as "commit README.md file" .
🌐
Nick Janetakis
nickjanetakis.com › blog › undo-remove-or-revert-specific-git-commits
Undo, Remove or Revert Specific Git Commits — Nick Janetakis
July 1, 2025 - If you happen to want to undo all commits on a specific branch but don’t want to rm -rf .git the whole repo you can run git update-ref -d HEAD. Optionally run git reset --hard right after to remove the changes on disk.
🌐
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 - To discard the last commit and its changes permanently, use git reset --hard HEAD~1 (with extreme caution).
🌐
OpenReplay
blog.openreplay.com › openreplay blog › undoing git commits after push: safely revert changes on remote repositories
Undoing Git Commits After Push: Safely Revert Changes on Remote Repositories
November 30, 2024 - git reset --hard HEAD~1: This command discards all changes from the last commit, effectively removing it from your repository.
🌐
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 - There's a way to undo—or, more accurately, amend—the most recent commit. We can amend the last commit by running the git commit --amend command.
🌐
Warp
warp.dev › terminus by warp › git › undoing git commits
How To Undo Your Last Git Commit(s) | Warp
November 30, 2023 - Using git reset is the most common way to undo changes that have been made to a repository. Resetting is a way of moving the current tip of a branch to a previous commit and thus resetting it to a previous state.
🌐
Reddit
reddit.com › r/git › how can i completely delete commits from both local and remote?
r/git on Reddit: How can I completely delete commits from both local and remote?
May 11, 2020 -

Hi there –

I've made a complete mess of my repo. Three branches are now in an unreliable state.

Unfortunately, much of the mess is already on GitHub.

I'd like to completely delete all commits that were made after a certain point, so that I can start over with a clean slate.

How can I do this, in a way that will also delete the problematic commits from GitHub?

Unfortunately, I'm not a very proficient Git user – so anything about rebasing, reflogs, etc. is going to lose me very quickly. Please, speak slowly and use small words :)

Thanks for any suggestions.

ETA: I found this StackOverflow answer, which recommends doing git reset --hard <last_working_commit_id> to delete the bad commits, and then using git push --force to delete them from GitHub. This sounds easy, but is there anything I should consider before using this technique?

🌐
Git
git-scm.com › book › en › v2 › Git-Basics-Undoing-Things
Git - Undoing Things
Luckily, git status tells you how to do that, too. In the last example output, the unstaged area looks like this: Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: CONTRIBUTING.md
🌐
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?

🌐
Atlassian
atlassian.com › git › tutorials › undoing changes › git revert
How to Revert a Commit in Git? | Atlassian Git Tutorial
Second, git revert is able to target an individual commit at an arbitrary point in the history, whereas git reset can only work backward from the current commit. 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 ...
🌐
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 - Running git reflog expire –expire=now –all removes old references. When Git runs GC, dangling commits are deleted permanently. ... This ensures all unreachable commits are deleted.
🌐
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 - You can also use the reset command to undo your last commit. But be careful – it will change the commit history, so you should use it rarely. It will move the HEAD, the working branch, to the indicated commit, and discard anything after: ... ...