Configure git to track only one file extension

2019-03-09 10:47发布

问题:

I have one directory tree with many kind of different files. There are 300 directories on the parent directory. Each directory could have other sub directories.

I only want to track *.cocci on all sub directories. Here is my .gitignore:

*
!*.cocci

But it do not work, as the files on sub directories are not tracked. How can I tell git that I only want to track *.cocci on all sub directories?

回答1:

Read this question.

You want:

# Blacklist everything
*
# Whitelist all directories
!*/
# Whitelist the file you're interested in. 
!*.cocci 

Note, this'll track only *.cocci files. Yours doesn't work because you ignore everything (that's the first line), which ignores all subdirectories.



回答2:

To extend @simont answer, if we wanted to whitelist *.cocci files of a given directory and its subdirectories I would wrongly have entered:

directory/*
!directory/*/
!directory/*.cocci 

That seems not to be including *.cocci files under directory/subtree.

In order to find all *.cocci files in all sub-directories of directory entered the following:

directory/**
!directory/*/
!directory/**/*.cocci 


回答3:

It is impossible to unignore directory by !dir in .gitignore, but then it is still possible to add directory by hand:

git add dir -f

So if the .gitignore file looks like:

*
!*.txt

Then when you do git add . new *.txt files are then added. New directory will not be auto-added, it have to be added by hand by git add dir -f



回答4:

Have you tried !*/*.cocci instead of your !*.cocci?