If you use only .dummy files as placeholders to make sure you always get these specific (almost empty) directories when doing a clean check, then you should probably just exclude /var and use git add -f var/.dummy var/log/.dummy to start direct tracking of placeholder files. The effective result is that you ignore everything except the .dummy files that you are explicitly tracking.
On the other hand, if you plan to expand the hierarchy of nearly empty directories beyond just var/log , then you can use a little explanation.
Rules are described in gitignore (5) , but sometimes it is difficult to understand all the rules and how they interact. An important role is that the last rule wins, and that later rules can only be effective if the directories in which they operate are not yet completely ignored. This means that if you ignored directories, you must ignore them before you can play out some of their contents (if you ignore the rest of their contents again).
If you want to automatically ignore everything except .dummy files in var , then the easiest way to do this is to place the .gitignore file in your var directory instead of doing it at the top level. I described this solution in another SO answer .
- Ignore everything in this directory (
var ). - But do not ignore this .gitignore file (you can leave this if you want to do
git add -f to start tracking this .gitignore file). - Also, do not ignore
.dummy files at any depth in the hierarchy embedded in this directory ( var ). - Also, do not ignore any directories at any depth in this directory (
var ).
Without this rule, the first rule will ignore subdirectories, and Git will never open them to search for .dummy files.
The last two patterns are "recursive" because they do not have a slash without an anchor (this means that * will correspond to the cosers in addition to other characters). The trailing slash makes the pattern match directories only. Leading and embedded slashes effectively bind the template to the location of the .gitignore file (or to the root of the repository for templates from other exception files). The idea is that a simple *.o should match anywhere, but dir/*.o should only match the elements immediately below dir (and we can use /*.o for this last effect in dir/.gitignore ).
If you cannot transfer the var/.gitinore , then you can still do what you requested, but you cannot automatically βunignoreβ new .dummy files .dummy in var without editing exception patterns.
# .gitignore at root of repository /var/* !/var/.dummy !/var/log/ /var/log/* !/var/log/.dummy
- Ignore everything in the
var directory, which is native to this .gitignore file. - But do not ignore
var/.dummy . - Also, do not ignore the
var/log directory. - But ignore everything directly in
var/log . - But do not ignore
var/log/.dummy .
Template: unignore is an interesting directory (skip this the first time, because by default everything is "unignred"), ignore what is in the directory, do not write the .dummy file to .dummy . Repeat the pattern for each deeper part of the hierarchy.
You can replace log with * so that it works for any directory directly under var , but it will not work automatically for deeper (for example, var/cache/.dummy ) directories, but not for var/log/ssh/.dummy ) This is due to the fact that we use inconspicuous slashes and bind patterns. To manually make it work, you must repeat the pattern to generate more exception rules for the deeper parts (unignore interesting dir, ignore the contents of the dir, unignore file).