Search code examples
gitversion-controlgitignore

.gitignore for directories at a specific depth?


I have directories at components/root/js/ and components/example/js/, and I don't want these to be tracked by Git.

However, I have a directory components/root/js2/plugins/js/, which I want to have tracked by Git.

I've looked up on this online and only found **, which I believe does not solve my problem.

My current .gitconfig contains the line:

js/

I've tried changing this to:

components/**/js/

But this matches for components/root/js/, components/example/js/, but also components/root/js2/plugins/js/ (a js folder below the second level of components - I only want to ignore js folders at the second level).

How can I achieve this goal?


Solution

  • OK, how about this:

    js/
    !components/root/js2/plugins/js/
    

    It will ignore all directories called js except for ones specified after !. In such scenario git will ignore components/root/js/ but won't ignore components/root/js2/plugins/js/.

    ** won't work because as it's said in man gitignore:

    A trailing "**" matches everything inside. For example, "abc/**" matches all files inside directory "abc", relative to the location of the .gitignore file, with infinite depth.