Configure git to track only one file extension

2019-03-09 10:34发布

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?

4条回答
smile是对你的礼貌
2楼-- · 2019-03-09 10:34

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

查看更多
叼着烟拽天下
3楼-- · 2019-03-09 10:40

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.

查看更多
forever°为你锁心
4楼-- · 2019-03-09 10:43

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

查看更多
Melony?
5楼-- · 2019-03-09 10:46

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 
查看更多
登录 后发表回答