5

Hi I want to undo my last commit. What I did is I made some changes to file then I commit them but I have not push them to main repo yet. After git commit -m "comment" command I ran git status and I got this message

Your branch is ahead of 'origin/demo' by 1 commit

So now I want to undo my last commit so how can I do that?

Om3ga
  • 24,135
  • 40
  • 122
  • 201
  • 1
    [Has been solved here][1] [1]: http://stackoverflow.com/questions/495345/git-removing-selected-commits-from-repository – Max Keller Mar 16 '12 at 13:11

2 Answers2

16

If you want to undo it completely:

git reset --hard HEAD^

If you want to undo it and keep your changes staged (before commit):

git reset --soft HEAD^

If you want to undo it and keep your files modified (before stage):

git reset --mixed HEAD^
ralphtheninja
  • 107,622
  • 20
  • 101
  • 118
  • Could you please tell me what is HEAD here? Should I use the same command that you typed here? – Om3ga Mar 16 '12 at 13:17
  • HEAD is a file that always refer to the last commit on the current branch, OR if you have checked out a commit (in a detached HEAD state) its value is that commit. Yes, you should use the command as is, assuming you haven't switched branch etc. – ralphtheninja Mar 16 '12 at 13:41
1

To revert your commit by creating another commit (assuming master is your working branch):

git revert master

To undo it, ie, pretend it never happened:

git reset --hard master~
me_and
  • 13,911
  • 6
  • 58
  • 93
koush
  • 2,962
  • 25
  • 31