can't git rm a directory

I accidentally added a directory to the list of items i'm supposed to git commit. Now when I try to run a git rm dirname, I get the error

rm 'dirname'
fatal: git rm: 'dirname': Is a directory

I tried the following commands git rm -rf dirname and git rm dirname --force, but none of these worked. They all produced the same error message.

How do I prevent dirname from being committed?

I am using git version 1.7.0.4

Thanks

Additional Notes

Ok, it appears git rm -rf dirname works only under some conditions. I ran some tests and normally it works. However, it will fail if dirname is itself a separately controlled git repository with a .git directory in it. I have this set up because dirname is a git controlled framework shared by many of my projects.


Solution 1:

Barring any permissions issues...

git rm --cached -r dirname

This should remove the directory from the staged commits.

At this point, it should be untracked and it may be a good idea to set it up to be ignored via gitignore.

Solution 2:

Never had this problem (so no promises that this will work and/or not destroy your data - working on a backup of your repository would be best!), but some options:


Option 1 (if you haven't committed locally):

  • git stash any valuable changes you've made.
  • git reset your tree (probably need to do git reset --hard HEAD)
  • git unstash your stashed changes and add/commit them carefully.

Option 2 (If you haven't committed locally, or if you have but there's a remote you haven't pushed to yet):

  • git clone a clean version of the repository
  • Carefully copy your changes over there
  • add/commit your changes carefully.

Option 3 (which makes a mess of your repo):

  • Commit your mistake to the repository.
  • Clean up using git rm -r

(You don't want to do this if the contents of that directory are huge, obviously.)