简体   繁体   中英

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

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.

You can put .gitigore files in any directory which will be a more reliable solution if the directories are moved later.

I would create 2 .gitignore files:

  • One at the project root /.gitignore which excludes js

    js

  • One in the plugins directory /components/root/js2/plugins/.gitignore which overrides root .gitignore

    !js

If the plugins directory will be moved later, eg to components/root/plugins the .gitignore will be moved too.

If you configure everything in the root .gitignore you must not forget to edit the root .gitignore if the directory is moved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM