Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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.

share|improve this question
What version of git are you using? What error message do you get when using git rm -rf dirname? – chuckx Apr 6 '11 at 15:46
runnign that command gave me same error as above. I am running git version – John Apr 6 '11 at 15:56
up vote 6 down vote accepted

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.

share|improve this answer
MUCH cleaner/easier than my solutions. – voretaq7 Apr 6 '11 at 16:52
WHY does git make an operation this simple so complicated? Thank you. – Justin Force Jan 27 '12 at 17:03
I'm getting fatal: pathspec 'lib' did not match any files unfortunately when running this command. – anon58192932 Feb 19 '15 at 20:36

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.