187

I did the following comments

git add /file1/path
git rm /file/path
git commit -m "message"

how do I undo my last commit using git?

Like I don't want to have those files committed.

3
  • 3
    You've got the answer here, after a 2sec search... ;-) stackoverflow.com/a/927386/1266697 Commented Oct 10, 2013 at 18:55
  • I'm unhappy with these questions and answers. They're often very vague. Does the user wish to simply undo the git commit (and leave the working tree unmodified?), or do they wish to also revert the working tree? Commented Aug 30, 2014 at 22:47
  • Explained in < 5 minutes in this video, if you prefer that format: youtube.com/watch?v=eg2xt-JoPfI&t=2s
    – ssmith
    Commented Dec 14, 2016 at 16:17

1 Answer 1

381

Warning: Don't do this if you've already pushed

You want to do:

git reset HEAD~

If you don't want the changes and blow everything away:

git reset --hard HEAD~
15
  • 56
    Dont do this if you already pushed
    – Arnold Roa
    Commented Dec 7, 2016 at 14:49
  • 1
    Please edit your answer, you just cost me a lot of files. I thought you meant remove all of GIT while trying to set up a new repository... Wow.
    – 1984
    Commented Jul 3, 2017 at 14:55
  • 6
    I do have a lack of understanding about git, but use it very often. I doubt I was the first, there should be a warning on the answer for idiots like me. I only wanted to reset git, not lose my directory (I had no prior commit to retrieve).
    – 1984
    Commented Jul 5, 2017 at 23:45
  • 5
    Doesn't work fatal: ambiguous argument 'HEAD~': unknown revision or path not in the working tree. Use '--' to separate paths from revisions
    – Green
    Commented Nov 5, 2017 at 12:36
  • 15
    "Don't do this if you've already pushed" - Why not? If you don't explain the intent, comments like that are what lead to crazy dogmas.
    – Pharap
    Commented Mar 27, 2018 at 18:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.