I accidentally added the wrong directory containing my files instead of adding a .java file. I added the directory containing the .class file. How can I undo this action?
git rm yourfiles/*.class
git commit -a -m "deleted all class files in folder 'yourfiles'"
or
git reset --hard HEAD~1
The hard reset to HEAD-1 will set your working copy to the state of the commit before your wrong commit.
http://www.kernel.org/pub/software/scm/git/docs/git-reset.html
Undo a commit and redo
$ git commit ... $ git reset --soft HEAD^ (1) $ edit (2) $ git commit -a -c ORIG_HEAD (3)
This is most often done when you remembered what you just committed is incomplete, or you misspelled your commit message, or both. Leaves working tree as it was before "reset".
Make corrections to working tree files.
"reset" copies the old head to .git/ORIG_HEAD; redo the commit by starting with its log message. If you do not need to edit the message further, you can give -C option instead.
Add/remove files to get things the way you want:
git rm classdir
git add sourcedir
Then amend the commit:
git commit --amend
The previous, erroneous commit will be edited to reflect the new index state - in other words, it'll be like you never made the mistake in the first place :)
Note that you should only do this if you haven't pushed yet. If you have pushed, then you'll just have to commit a fix normally.
Make any changes you need to, then:
If it's a private branch you can amend
the commit:
git commit --amend
If it's a shared branch you'll have to make a new commit:
git commit -m 'Remove accidental .class files.'
git push
You also may want to think about using a global gitignore to stop this kind of thing happening again.