Multiple .gitignore files versus .gitkeep files

I have been a devout git user for as long as I have been programming. I was lucky enough to bypass subversion entirely, at least that is what my friends who’ve used svn have told me.

The cool thing about git is that it tracks changes, not files or directories. When I create a new empty directory that directory is not tracked by git because it has no file changes inside, hence, the directory will never make it into git until I add a file to that directory. For this example I am going to use the logs directory of a virtual site. In my logs directory I have two files, an error_log and an access_log. Apache error logs and access logs should never be tracked by version control because they are logs that are specific to the given system the site is installed on. However, we should try to track the logs directory in version control. As hinted at before, we can do this in one of two ways.

First, the .gitignore file. We can add as my .gitignore files as we need. You can place these files in any directory you want tracked that wouldn’t be already (technically you can put them anywhere you want and have as many as you want, but don’t go excessive). .gitignore files are technically the preferred way to go about tracking empty directories because these files are actually read by git. The .gitignore file structure is pretty simple, for defining how to ignore the contents of the logs file you just need to tell git the relative path (relative to the path of the .gitignore file). .gitignore file:

# log files
logs/access_log
logs/error_log

How to track the empty directory with .gitignore

So, as you can see this is pretty simple, but I think that multiple .gitignore files can become confusing.

Second, the .gitkeep file. This is a git hack that was first adopted by the Rails community. .gitkeep files are not an official file recognized by git. That said, because they start with a '.' they will be a hidden file in any system as well as have it’s changes tracked by git. Technically our file name could be .foobar and it would still work. The reason I like the .gitkeep files is because it’s purpose is directly implied by the name of the file (keep the directory in git even if it’s directory is empty). Another reason I like the use of .gitkeep files over .gitignore is I like to keep all ignored files in one spot, it can be hard to track what is ignored if there are multiple .gitignore files at work in a system. The .gitkeep file at work:

Pretty simple right? So, I have my preference, what is yours?