How to undo the last commit in git
274,939
Solution 1
I think you haven't messed up yet. Try:
git reset HEAD^
This will bring the dir to state before you've made the commit, HEAD^
means the parent of the current commit (the one you don't want anymore), while keeping changes from it (unstaged).
Solution 2
Try simply to reset last commit using --soft
flag
git reset --soft HEAD~1
Note :
For Windows, wrap the HEAD parts in quotes like git reset --soft "HEAD~1"
Related videos on Youtube
Author by
chintan s
Updated on July 08, 2022Comments
-
chintan s almost 2 years
By mistake, I did
git add .
andgit commit
in thedevelop
branch. But luckily, I did not dogit push
.So I wanted to revert it back to original state.
I tried
git reset --soft
andgit reset HEAD --hard
but looks like I have messed it up.How do I fix this? I want to go back to original state and possibly keep the code changes.
-
geneorama almost 7 yearsThis might be a duplicate, but this is the over simplistic answer that I want 90% of the time. Thank you
-
Amal Gupta almost 7 yearsYou, sir/ma'am, are a savior!
-
nfriend21 over 6 yearsAfter you do this, if you want to completely remove the unstaged changes, you'll need to run the following: git reset --hard HEAD
-
Green over 6 yearsDon't know why this answer marked as correct. I get error thrown
fatal: ambiguous argument 'HEAD^': unknown revision or path not in the working tree. Use '--' to separate paths from revisions,
Doesn't work -
Green over 6 years
fatal: ambiguous argument 'HEAD~1': unknown revision or path not in the working tree. Use '--' to separate paths from revisions
-
Jose over 6 years@Green same for me. I used this command instead to completely remove last commit:
git reset --hard HEAD~1
Found here -
Boop over 6 yearsif
fatal: ambiguous argument
simply usegit reset -- HEAD^
-
bart over 6 yearsOn windows, the caret is a special character for CMD (it's used to escape a character, similar to the backslash in Linux) so you better quote "HEAD^".
-
Fred over 6 yearsTry using git bash. If you're using Windows the console won't work for some reason, not sure why. Git bash does tho.
-
user56reinstatemonica8 over 6 years@Green Windows doesn't like
HEAD~1
, wrap it in quotes likegit reset --soft "HEAD~1"
-
alexwc_ almost 6 years95% of the time this works 100%... too bad I didn't find it a month ago! Simple, which is what I need.
-
Darina Sergeivna over 3 yearsQuestion - and if i have done add . commit and the pull , i believe that this might effect on other files too and not only on mine?