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 OverflowIt'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
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.
version control - How do I undo the most recent local commits in Git? - Stack Overflow
Is it possible to undo the last Git commit but keep the code changes? - Ask a Question - TestMu AI (formerly LambdaTest) Community
How do I undo the most recent local commits in Git? [Tutorial]
How to undo pushed commits
Videos
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!
Think we have code.txt file.
We make some changes on it and commit.
We can undo this commit in three ways, but first you should know what is the staged file...
An staged file is a file that ready to commit and if you run git status this file will be shown with green color and if this is not staged for commit will be shown with red color:

It means if you commit your change, your changes on this file is not saved.
You can add this file in your stage with git add code.txt and then commit your change:

Undo last commit:
Now if we want to just undo commit without any other changes, we can use
git reset --soft HEAD^
If we want to undo commit and its changes (THIS IS DANGEROUS, because your change will lost), we can use
git reset --hard HEAD^
And if we want to undo commit and remove changes from stage, we can use
git reset --mixed HEAD^or in a short formgit reset HEAD^
Usually, you want to undo a commit because you made a mistake and you want to fix it - essentially what the OP did when he asked the question. Really, you actually want to redo a commit.
Most of the answers here focus on the command line. While the command line is the best way to use Git when you're comfortable with it, its probably a bit alien to those coming from other version control systems to Git.
Here's how to do it using a GUI. If you have Git installed, you already have everything you need to follow these instructions.
NOTE: I will assume here that you realised the commit was wrong before you pushed it. If you don't know what pushing means, then you probably haven't pushed. Carry on with the instructions. If you have pushed the faulty commit, the least risky way is just to follow up the faulty commit with a new commit that fixes things, the way you would do it in a version control system that does not allow you to rewrite history.
That said, here's how to fix your most recent fault commit using a GUI:
- Navigate to your repository on the command line and start the GUI with
git gui - Choose "Amend last commit". You will see your last commit message, the files you staged and the files you didn't.
- Now change things to how you want them to look and click Commit.