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?

mortsahl
  • 602
  • 6
  • 18

1 Answers1

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.

Ismail Badawi
  • 36,054
  • 7
  • 85
  • 97
  • 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 Jun 01 '14 at 22:43
  • @mortsahl You're ignoring `node_modules`; you need to write `node_modules/*` to ignore just its children. – Ismail Badawi Jun 01 '14 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 Jun 01 '14 at 23:15
  • Got it -- since it was a hidden directory I wanted to not be ignored I had to ` node_modules/.*/ ` – mortsahl Jun 01 '14 at 23:30