How to write a rule in gitignore to ignore all files except directories in some directory?

So let's say I have such a directory structure

uploads/ --dir1/ ----one.txt ----two.txt ----dir2/ ------one.txt ------two.txt 

I would like any directories and auxiliary directories of downloaded files to be tracked, but not the files in it, except for a dummy file, because git does not track directories.

I would think that something like this would work, but it is not.

 * !.gitignore 
+6
git gitignore
source share
1 answer

When you specify to ignore * , Git will not return to any subdirectories (even if you have a "unignore" template that may correspond to something inside one of the directories).

There is little gitignore template that can help. The trailing slash makes the template apply only to directories. So you should be able to use this:

 # .gitignore * !.keep !*/ 

In the demo below, I use the above .gitignore and have an additional file (named do-not-keep ) next to each .keep file. You can see that it works for several levels of subdirectories and does not display other files. You will need to organize for each directory your own .keep (or any other) through some independent method.

 % git status --short -uall ?? a/.keep ?? a/b/.keep ?? a/b/c/.keep ?? a/b/c/d/.keep ?? e/.keep ?? e/f/.keep ?? e/f/g/.keep ?? h/.keep ?? h/i/.keep ?? j/.keep 

This was done with Git 1.7.3.1, but I expect it to work for other versions as well.

Demo setup:

 % git init % mkdir -pa/b/c/de/f/gh/ij % zargs -i.. -- **/*(/) -- touch ../.keep ../do-not-keep % tree -aI .git . . |-- .gitignore |-- a | |-- .keep | |-- b | | |-- .keep | | |-- c | | | |-- .keep | | | |-- d | | | | |-- .keep | | | | `-- do-not-keep | | | `-- do-not-keep | | `-- do-not-keep | `-- do-not-keep |-- e | |-- .keep | |-- do-not-keep | `-- f | |-- .keep | |-- do-not-keep | `-- g | |-- .keep | `-- do-not-keep |-- h | |-- .keep | |-- do-not-keep | `-- i | |-- .keep | `-- do-not-keep `-- j |-- .keep `-- do-not-keep 10 directories, 21 files 
+7
source share

All Articles