It's as simple as this:

git reset HEAD^

Note: some shells treat ^ as a special character (for example some Windows shells or ZSH with globbing enabled), so you may have to quote "HEAD^" or use HEAD~1 in those cases.

git reset without a --hard or --soft moves your HEAD to point to the specified commit, without changing any files. HEAD^ refers to the (first) parent commit of your current commit, which in your case is the commit before the temporary one.

Note that another option is to carry on as normal, and then at the next commit point instead run:

git commit --amend [-m … etc]

which will instead edit the most recent commit, having the same effect as above.

Note that this (as with nearly every git answer) can cause problems if you've already pushed the bad commit to a place where someone else may have pulled it from. Try to avoid that

Answer from Gareth on Stack Overflow
Top answer
1 of 14
3396

It's as simple as this:

git reset HEAD^

Note: some shells treat ^ as a special character (for example some Windows shells or ZSH with globbing enabled), so you may have to quote "HEAD^" or use HEAD~1 in those cases.

git reset without a --hard or --soft moves your HEAD to point to the specified commit, without changing any files. HEAD^ refers to the (first) parent commit of your current commit, which in your case is the commit before the temporary one.

Note that another option is to carry on as normal, and then at the next commit point instead run:

git commit --amend [-m … etc]

which will instead edit the most recent commit, having the same effect as above.

Note that this (as with nearly every git answer) can cause problems if you've already pushed the bad commit to a place where someone else may have pulled it from. Try to avoid that

2 of 14
430

There are two ways of handling this. Which is easier depends on your situation.

Reset

If the commit you want to get rid of was the last commit, and you have not done any additional work you can simply use git-reset

git reset HEAD^

Takes your branch back to the commit just before your current HEAD. However, it doesn't actually change the files in your working tree. As a result, the changes that were in that commit show up as modified - its like an 'uncommit' command. In fact, I have an alias to do just that.

git config --global alias.uncommit 'reset HEAD^'

Then you can just used git uncommit in the future to back up one commit.

Squashing

Squashing a commit means combining two or more commits into one. I do this quite often. In your case you have a half done feature commited, and then you would finish it off and commit again with the proper, permanent commit message.

git rebase -i <ref>

I say above because I want to make it clear this could be any number of commits back. Run git log and find the commit you want to get rid of, copy its SHA1 and use it in place of <ref>. Git will take you into interactive rebase mode. It will show all the commits between your current state and whatever you put in place of <ref>. So if <ref> is 10 commits ago, it will show you all 10 commits.

In front of each commit, it will have the word pick. Find the commit you want to get rid of and change it from pick to fixup or squash. Using fixup simply discards that commits message and merges the changes into its immediate predecessor in the list. The squash keyword does the same thing, but allows you to edit the commit message of the newly combined commit.

Note that the commits will be re-committed in the order they show up on the list when you exit the editor. So if you made a temporary commit, then did other work on the same branch, and completed the feature in a later commit, then using rebase would allow you to re-sort the commits and squash them.

WARNING:

Rebasing modifies history - DONT do this to any commits you have already shared with other developers.

Stashing

In the future, to avoid this problem consider using git stash to temporarily store uncommitted work.

git stash save 'some message'

This will store your current changes off to the side in your stash list. Above is the most explicit version of the stash command, allowing for a comment to describe what you are stashing. You can also simply run git stash and nothing else, but no message will be stored.

You can browse your stash list with...

git stash list

This will show you all your stashes, what branches they were done on, and the message and at the beginning of each line, and identifier for that stash which looks like this stash@{#} where # is its position in the array of stashes.

To restore a stash (which can be done on any branch, regardless of where the stash was originally created) you simply run...

git stash apply stash@{#}

Again, there # is the position in the array of stashes. If the stash you want to restore is in the 0 position - that is, if it was the most recent stash. Then you can just run the command without specifying the stash position, git will assume you mean the last one: git stash apply.

So, for example, if I find myself working on the wrong branch - I may run the following sequence of commands.

git stash
git checkout <correct_branch>
git stash apply

In your case you moved around branches a bit more, but the same idea still applies.

🌐
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 - It moves the HEAD pointer back one commit and unstages the changes but keeps them in your working directory. git reset --hard HEAD~1: This command discards all changes from the last commit, effectively removing it from your repository.
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
version control - How do I undo the most recent local commits in Git? - Stack Overflow
There are two ways to "undo" your ... to your remote repository): Let's say I committed locally, but now I want to remove that commit. git log commit 101: bad commit # Latest commit. This would be called 'HEAD'. commit 100: good commit # Second to last commit. This is the one we want. To restore everything back to the way it was prior to the last commit, we need to reset to the commit before HEAD: git reset --soft HEAD^ # Use --soft if you want to keep your changes git reset ... More on stackoverflow.com
🌐 stackoverflow.com
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
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
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 ...
🌐
Graphite
graphite.com › guides › git-undo-last-commit
Git undo last commit - Graphite
... 2. Undo the last commit but keep the changes: If you want to undo the commit but keep the changes in your working directory, use the git reset command with the --soft flag, followed by HEAD~.
🌐
KodeKloud
kodekloud.com › blog › git-uncommit-last-commit
How to Uncommit Last commit in Git (5 Scenarios)
November 25, 2025 - To undo the act of committing and also unstage your changes but keep your files intact, use git reset HEAD. To undo everything, including discarding your changes and resetting your files to the previous commit, use git reset --hard HEAD.
🌐
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
When you want to undo your last local commit — one that hasn't been pushed to a remote yet — git reset is the right tool: ... This rewinds the branch pointer by one commit. The --soft flag preserves your changes as staged modifications, ...
Published   5 days ago
🌐
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 - The git reset command can be used to undo recent commits and manage commit history, but be cautious as it can overwrite commit history, especially for changes pushed to a remote repository.
Find elsewhere
🌐
GitHub
gist.github.com › vishaltelangre › 5531806
Revert last local or remote commit (git) - Gist - GitHub
There are two ways to "undo" your ... way it was prior to the last commit, we need to reset to the commit before HEAD: git reset --soft HEAD^ # use --soft if you want to keep your changes git reset --hard HEAD^ # use --hard if ...
🌐
Adamdurrant
adamdurrant.co.uk › blog › undo-git-commit
How to Undo Pushed Git Commits Locally & Remotely<!-- --> | ADurrant
While in insert mode, simply change the word pick to drop. For example, lets drop the third and fifth commit like so: pick 3e67142 Second commit drop 85c2865 Third commit pick f0dcf05 Fourth commit drop f0ab22d Fifth commit pick 3a96ff4 Sixth commit · Now hit esc and then type :wq to save and quit vim. ... $ git status interactive rebase in progress; onto fbcf6b2 Last commands done (2 commands done): pick 3e67142 Second commit drop 85c2865 Third commit Next commands to do (3 remaining commands): pick f0dcf05 Fourth commit drop f0ab22d Fifth commit (use "git rebase --edit-todo" to view and edit) You are currently editing a commit while rebasing branch 'master' on 'fbcf6b2'.
🌐
LabEx
labex.io › tutorials › git-how-to-undo-git-commit-but-keep-changes-392512
How to Undo Git Commit But Keep Changes | LabEx
For example, you could create an alias for git reset --soft HEAD~1 to quickly undo the most recent commit without discarding your changes. As a general best practice, it's always a good idea to regularly back up your Git repository, either by ...
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.

🌐
DataCamp
datacamp.com › blog › git-undo-last-commit
Git Undo Last Commit: Step-by-Step Guide for Beginners | DataCamp
June 23, 2025 - Run git revert HEAD~1. This creates a new commit that undoes the changes from the second-to-last commit (because HEAD~1 points to it). It doesn’t delete anything, instead it reverses the changes in a clean, trackable way. Once you’ve done that, don’t forget to push the fix to the remote ...
🌐
DevToolbox
devtoolbox.dedyn.io › home › blog › undo last commit in git
How to Undo Last Commit in Git (Keep, Unstage, or Discard Changes) | DevToolbox Blog
February 26, 2026 - Run git log --oneline -n 5 and copy the last known-good commit hash. Inspect the exact per-file patch in Git Diff Viewer so you can confirm you are undoing only the intended changes.
🌐
Reddit
reddit.com › r/git › undoing last commit
Undoing last commit : r/git
January 7, 2021 - You could delete the commit in your local devel branch, then push --force ... to the corresponding remote origin branch. That way your commit history is not cluttered with reverts ... You can revert as others have said, and I generally think that’s the best method.
🌐
haikel-fazzani
haikel-fazzani.eu.org › devops › git-undo-recent-commits
Undo the Most Recent Local & Remote Git Commits
July 20, 2025 - When you’ve already pushed your changes to a remote branch, or are collaborating within a team, git revert is your go-to solution. It ensures a clean, traceable rollback by creating a new commit that nullifies the effects of a specified previous ...
🌐
freeCodeCamp
freecodecamp.org › news › how-to-undo-changes-in-git-e1da7930afdb
How to undo changes in Git
December 7, 2018 - If you have a commit that has been pushed into the remote branch, you need to revert it. Reverting means undoing the changes by creating a new commit. If you added a line, this revert commit will remove the line. If you removed a line, this revert commit will add the line back. ... Make sure commit the changes is checked. ... GitHub has a useful article that shows you how to undo almost everything with Git.
🌐
ITU Online
ituonline.com › blogs › how-to-undo-a-git-commit-before-pushing-to-remote-2
How To Undo A Git Commit Before Pushing To Remote – ITU Online IT Training
June 16, 2026 - Decide whether to keep, restage, or discard the changes. Use git commit --amend for the last commit if only the message or files need fixing. Use git reset --soft HEAD~1 to undo the commit and keep changes staged.
🌐
GitLab
docs.gitlab.com › topics › git › undo
Revert and undo changes | GitLab Docs
For more information about purging ... 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....
🌐
Melvin George
melvingeorge.me › blog › undo-last-commit-in-remote-git-repo
How to undo the last commit from a remote git repository? | MELVIN GEORGE
December 31, 2020 - To undo the last commit from a remote git repository, you can use the git reset command.
🌐
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. This will move the changes from the bad commit into the Index, and the commit message will be set to the bad commit's message.