Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

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 1.7.0.4 –  John Apr 6 '11 at 15:56
add comment

2 Answers

up vote 4 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. –  sidewaysmilk Jan 27 '12 at 17:03
add comment

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
add comment

Your Answer

 
discard

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.