Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make 'git not ignore' certain file extension in nested folder structure? [duplicate]

Tags:

git

Say I have a folder structure like this:

├── a
│   ├── b
│   │   ├── c.xml
│   │   └── d.c
│   ├── v
│       ├── e.class
│       └── g.c
|
├── m
│   ├── p
│   │   ├── w.out
│   │   └── x.c
│   ├── q
│   │   ├── y.mp3
│   │   └── z.c

And I only want to not ignore *.c.

I tried

*                                                                         
!*.c 

in .gitignore, but that seems only work in the current level.

What I want is to set something like !*/*/*.c, since there may be other directories added to the git repo later, I can't hard-code the .gitignore file.

like image 864
Hao Tan Avatar asked Oct 20 '14 15:10

Hao Tan


1 Answers

Add !*/ to your .gitignore to allow directories:

*
!*/
!*.c

From gitignore man page (emphasis mine):

An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn't list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined. Put a backslash ("\") in front of the first "!" for patterns that begin with a literal "!", for example, "!important!.txt".

That's why you have to exclude the directories.

like image 186
Tim Zimmermann Avatar answered Oct 07 '22 00:10

Tim Zimmermann