简体   繁体   中英

git ignore all but one type of file, anywhere down

My goal is to write a .gitignore file that will ignore everything at any level downwards except:

*.xxx
*.yyy

in the current directory (where the .gitignore is located) and

*.xxx

anywhere -at any level- in the tree below.

In other words, I want to keep in git *.xxx at any level as well as *.yyy at the 'root' level. Anything else should be ignored.

The name of the subfolders is unknown, so I cannot quote them in the gitignore file.

I have tried many alternatives, with no success so far: For instance:

#ignore everything in any subdirectory
*
*/**
#expect some files:
!*.yyy
!*/**/*.xxx

I think I am hitting the restriction:

"It is not possible to re-include a file if a parent directory of that file is excluded"

in the git ignore doc... Is there a way to work-around this...?

The problem is indeed with the blacklisting of all the subdirectories at first and afterwards trying to re-include some of the content within those directories.

The solution to this is whitelisting the subdirectories again before trying to re-include the files:

# blacklist everything
*

# whitelist all content ending with slash (directories)
!/**/

# finally whitelist all files with valid extension in the entire repo
!*.xxx
!*.yyy
# ignoring the ones that are only allowed within the repo root
*/**/*.yyy

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