188

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.

Kermit the Frog
  • 3,949
  • 7
  • 31
  • 39
  • 3
    You've got the answer here, after a 2sec search... ;-) http://stackoverflow.com/a/927386/1266697 – Jeje Doudou Oct 10 '13 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? – Aaron McDaid Aug 30 '14 at 22:47
  • Explained in < 5 minutes in this video, if you prefer that format: https://www.youtube.com/watch?v=eg2xt-JoPfI&t=2s – ssmith Dec 14 '16 at 16:17

1 Answers1

382

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~
topher
  • 14,790
  • 7
  • 54
  • 70
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • 56
    Dont do this if you already pushed – Arnold Roa Dec 07 '16 at 14:49
  • @ArnoldRoa Why? Can you expand? – Mark A Jun 29 '17 at 18:33
  • 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 Jul 03 '17 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 Jul 05 '17 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 Nov 05 '17 at 12:36
  • 1
    @Green That error occurs when there's only single commit. In that case, its just easier to rm -rf the .git directory and create new repository from scratch. – Deepak Mittal Mar 15 '18 at 09:07
  • 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 Mar 27 '18 at 18:06
  • @Pharap then git revert is the way to do it. See [https://stackoverflow.com/questions/22682870/git-undo-pushed-commits?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa] – Fookiee Apr 20 '18 at 15:44
  • @Fookiee The effects are different though. That adds a new commit over top rather than deleting an old commit. That does not explain why deleting an already pushed commit is bad. – Pharap Apr 20 '18 at 16:18
  • @Pharap Correct. Deleting an already pushed commit is bad when you are not the only contributor. Someone else - or a Service/Bot/Script - may have seen that commit already. After you have pushed some changes, the changes may be already "used" by someone else. When you add a new commit reverting something everyone will understand, that this is a later change. – Fookiee May 09 '18 at 14:33
  • @Fookiee But if the commit is already in the remote repository, your push will just fail and you will need to pull first, right? – johndodo May 15 '18 at 09:09
  • @johndodo that depends on your remote server and user privileges. Many allow a push -f – Fookiee Aug 08 '18 at 09:21
  • # DON'T USE THIS, YOU WILL LOOSE EVERYTHING – Nixon Kosgei Oct 25 '18 at 04:40
  • I just used this and I didn't lose everything. If you're not sure what you're doing, you can always make a copy of your `.git` folder. The accepted answer to the suggested duplicate question is much more useful – user1007074 Dec 25 '21 at 22:15
  • 1
    @Green See answers at https://stackoverflow.com/questions/6632191/how-to-revert-initial-git-commit – Steve Smith Jan 10 '22 at 20:03