tags:

views:

18718

answers:

4

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?

+3  A: 
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.

Lennart
"--hard" will get rid of the modified .java files in the working directory that he wanted to commit.
Esko Luontola
Despite the downvotes, I'm glad this answer is here.
Gavin Schultz-Ohkubo
+66  A: 

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)
  1. 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".

  2. Make corrections to working tree files.

  3. "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.

Esko Luontola
And if the commit was to the wrong branch, you may `git checkout theRightBranch` with all the changes stages. As I just had to do.
Frank Shearar
+23  A: 

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.

bdonlan
+4  A: 

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.

Josh