I would like for git to ignore all files except those with a few different extensions buried somewhere within a specific set of subdirectories.
For examples, say I have a directory with the following sub-directories:
subdir1
subdir2
subdir3
subdir4
subdir5
...
Each subdirectory contains an arbitrary number of nested sub-directories. Files with extension .txt
or .pdf
may be present in some of those nested sub-directories. I would like to have the repo track only the .txt
and .pdf
files somewhere within subdir2
and subdir3
and no other files.
Please explain why your solution works.
If you want to ignore ALL files expect all .pdf
and .txt
files in the directory subdir2/
and subdir3/
, add following to gitignore
:
# Ignore everything
*
!*/
# But not these files
!.gitignore
!subdir2/**/*.pdf
!subdir2/**/*.txt
!subdir3/**/*.pdf
!subdir3/**/*.txt
subdir/**
matches all files and directories in any subdir/
directory and all of its subdirectories. The !
negates the pattern where follows. And so you can ignore all except the .pdf
and .txt
files in the wished directories.
The folder-structure is as follow:
|-- folder
|-- .git
|-- .gitignore
|-- subdir1/
|-- <folder content>
|-- subdir2/
|-- <folder content>
|-- subdir3/
|-- <folder content>
|-- subdir4/
|-- <folder content>
|-- subdir5/
|-- <folder content>