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.

Discussions

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
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
Is it better to use commit instead of stash when temporarily checking out a branch?
I've completely given up on using stash. There's just no point in maintaining an unstructured list of unrelated changes. git commit -am "WIP" switch branch, do your thing, switch back git reset HEAD~1 continue where you left off. There's cherry-pick or rebase, if you need the changes elsewhere. More on reddit.com
🌐 r/git
25
8
December 26, 2023
Easiest way to deal with being not able to pull because of locally modified files?
In this situation, your students should: git pull --rebase You are characterizing "forgetting to pull" as if it's a blunder (and maybe in this exact scenario it is)... but version control is all about enabling concurrent development on the same code base between multiple developers. Having local changes that have diverged from the upstream is a super-common, everyday occurrence. It's not necessarily a bad state that needs to be fixed, it's often just a normal consequence of collaborating on code. Yes, in your scenario you've described a single student, but he's one of a group of four, so I'm assuming they collaborate. Additionally, learning how to reconcile a branch that has diverged is a core git skill that should be embraced IMO. The command above basically checks out origin's version of the current branch, then replays all of the commits in the local repo on top of it. Conflicts can happen, of course. (And resolving them is another "core git skill", IMO). More on reddit.com
🌐 r/git
16
10
June 9, 2023
People also ask

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 ...
🌐
Reddit
reddit.com › r/git › new to git, advice needed. revert commit but keep changes
r/git on Reddit: New to git, advice needed. Revert commit but keep changes
November 18, 2023 -

Hello everyone! I'm working on a project in a team. I'm using GUI git client Fork, while also learning command line git.
I finished a task and made a commit (not pushed), but soon realized that I didn't get it done properly, and that I needed to put more work into it. So I left my initial commit as it was, and just kept on working on the task. Right now I'm finally finishing it, but I'm asked to make only one commit with the whole task in it.

When I try to Revert the initial commit, it gives me an error:

Your local changes to the following files would be overwritten by merge
Revert failed

What should I do to keep the changes in the initial commit and reapply the new ones? I may have rewritten some code from the initial commit, but not fully, so some parts must stay, and the new changes to be added.

Thank you for your attention!

🌐
Graphite
graphite.com › guides › git-undo-last-commit
Git undo last commit
... 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~.
🌐
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 - Use git revert instead, which safely undoes changes while preserving history. When working with local commits or feature branches, it is generally safe to use git reset, git commit –amend, or git rebase, as these are not shared.
Find elsewhere
🌐
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 - If you want to keep the changes but undo the last commit, use git reset --soft HEAD~1. 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 - Use `git revert -m 1 <merge-commit-hash>` to revert a merge commit, specifying the parent number (`-m 1`) to keep. What happens if I encounter conflicts while reverting a commit? Resolve the conflicts manually, stage the changes, and then run ...
🌐
JetBrains
jetbrains.com › guide › tips › undo-last-commit
Undo your Last Commit - JetBrains Guide
February 17, 2023 - It shows the list of commits on the current branch. The most recent one is at the top. Right-click that commit and in the pop-up window, choose Undo Commit. That commit has some changes. Let’s keep them in the default changelist.
🌐
KodeKloud
kodekloud.com › blog › git-uncommit-last-commit
How to Uncommit Last commit in Git (5 Scenarios)
November 25, 2025 - To see the history of your commits ... those changes with others or merge them to the main branch. You can use git revert <commit_hash> --no-edit to undo your commit and create a new commit that reverses the changes of the previous ...
🌐
CloudBees
cloudbees.com › blog › git-undo-commit
How to Undo Changes in Git with Git Undo Commit
November 25, 2020 - If you must undo changes in a shared branch of a shared repo, the best tool to use is git revert <commit id>. It reverts the changes done by the commit you specified, and then it creates a new commit for it.
🌐
GitHub
gist.github.com › wilsonsilva › c6f870e6423a1c0076224d1f1e468dbb
Undo last commit but keep changes · GitHub
Save wilsonsilva/c6f870e6423a1c0076224d1f1e468dbb to your computer and use it in GitHub Desktop. Download ZIP · Undo last commit but keep changes · Raw · undo_last_commit.sh · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below.
🌐
Git
git-scm.com › book › ms › v2 › Git-Basics-Undoing-Things
Git - Undoing Things
You end up with a single commit — the second commit replaces the results of the first. The next two sections demonstrate how to work with your staging area and working directory changes. 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.
🌐
Sentry
sentry.io › sentry answers › git › undo the most recent local git commits
Undo the most recent local Git commits | Sentry
The following commands will undo and redo the most recent commit: git add . git commit -m "This commit is a mistake" git reset HEAD~ git add main.py # need to re-add files after reset git commit -m "This commit corrects the mistake" ... git ...
🌐
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 - The command above will undo the changes by creating a new commit and reverting that file to its previous state, as if it never changed. Lastly, use git push to push the change to the remote branch. Once you do that, you will see that the commit message will be the same as the previous one but with the word revert preceding it, such as 'Revert "commit README.md file"'. Keep in mind that the commit history will show both commits separately:
🌐
Warp
warp.dev › terminus by warp › git › undoing git commits
How To Undo Your Last Git Commit(s) | Warp
November 30, 2023 - If you decide to use git reset or git checkout to overwrite commits, then you can use git push --force to overwrite the remote branch with your local branch. This will overwrite any commits that were made after your last pull.
🌐
Git
git-scm.com › docs › git-reset
Git - git-reset Documentation
Running git reset --hard ORIG_HEAD ... git reset --merge keeps your local changes. ... Suppose you are interrupted by an urgent fix request while you are in the middle of a large change. The files in your working tree are not in any shape to be committed yet, but you need to ...
🌐
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 - Your latest commit will now be undone. Your changes remain in place, and the files go back to being staged (e.g., with Git add) so you can make any additional changes or add any missing files. You can then make a new commit. If you have made local commits that you don't like, and they have not been pushed yet you can reset things back to a previous good commit.
🌐
Quora
quora.com › How-do-you-undo-a-specific-git-commit-without-undoing-more-recent-ones
How to undo a specific git commit without undoing more recent ones - Quora
Answer (1 of 6): Seems like the wrong model for working. You should have a branch each to work on, and not be able commit things that affect other people’s branches. One branch per student for each project. Branches in git are “cheap”, so won’t change the amount of storage compared ...
🌐
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.
🌐
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 ...