1

I want to ignore all files in a directory but not any children directories and their contents. For instance ...

parentdir
   childdir
      filea
      fileb
   file1
   file2
   file3

So, I want to ignore file1, file1, file3 but keep everything else. How do I do that?

0

1 Answer 1

4

You can use a leading ! to negate a pattern, reincluding any names excluded by previous patterns. This should work:

parentdir/*
!parentdir/*/

This says to exclude everything inside parentdir (but not parentdir itself), but then don't exclude any directories -- a trailing slash will match directories but not files.

4
  • Thanks... but either it didn't work or I didn't follow your instructions correctly. Here's the content of my .gitignore file (I don't know how to format a comment) ... .tmp .idea node_modules !node_modules/.bin/
    – mortsahl
    Commented Jun 1, 2014 at 22:43
  • @mortsahl You're ignoring node_modules; you need to write node_modules/* to ignore just its children. Commented Jun 1, 2014 at 22:47
  • Thanks again -- still didn't work. I'm now using code .tmp .idea node_modules/* !node_modules/.bin/ Checked in to new Bitbucket repository, then clone to a new directory. node_modules was ignored ... but so was node_modules/.bin/
    – mortsahl
    Commented Jun 1, 2014 at 23:15
  • Got it -- since it was a hidden directory I wanted to not be ignored I had to ` node_modules/.*/ `
    – mortsahl
    Commented Jun 1, 2014 at 23:30

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