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"

Share:
274,939

Related videos on Youtube

chintan s
Author by

chintan s

Updated on July 08, 2022

Comments

  • chintan s
    chintan s almost 2 years

    By mistake, I did git add . and git commit in the develop branch. But luckily, I did not do git push.

    So I wanted to revert it back to original state.

    I tried git reset --soft and git 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
    geneorama almost 7 years
    This might be a duplicate, but this is the over simplistic answer that I want 90% of the time. Thank you
  • Amal Gupta
    Amal Gupta almost 7 years
    You, sir/ma'am, are a savior!
  • nfriend21
    nfriend21 over 6 years
    After you do this, if you want to completely remove the unstaged changes, you'll need to run the following: git reset --hard HEAD
  • Green
    Green over 6 years
    Don'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
    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
    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
    Boop over 6 years
    if fatal: ambiguous argument simply use git reset -- HEAD^
  • bart
    bart over 6 years
    On 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
    Fred over 6 years
    Try using git bash. If you're using Windows the console won't work for some reason, not sure why. Git bash does tho.
  • user56reinstatemonica8
    user56reinstatemonica8 over 6 years
    @Green Windows doesn't like HEAD~1, wrap it in quotes like git reset --soft "HEAD~1"
  • alexwc_
    alexwc_ almost 6 years
    95% of the time this works 100%... too bad I didn't find it a month ago! Simple, which is what I need.
  • Darina Sergeivna
    Darina Sergeivna over 3 years
    Question - 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?