Last Updated: January 28, 2019
·
1.573K
· calamari

How to track a directory but not it's files with git

Sometimes it is needed to add a directory in git but you do not want to track any files in it. For example if you have a log directory. You probably do not want to add the actual log files into your git, but adding the directory could make sense, to avoid warning such like this:

DEPRECATION WARNING: Automatic directory creation for '/home/user/my-app/log/development.log' is deprecated.  Please make sure the directory for your log file exists before creating the logger.

There is no official way to do just that in git. Git can not track empty directories. So if you happen to have a line like this in your .gitignore file:

log/*.*

it won't add the log dir into the repository. But an inofficial convention is to just add an empty file named .gitkeepto this directory and add this to git. It does nothing, but the naming tells other readers, that it has to do something with git, and searching for ".gitkeep" will give the future developer the anwser he needs.

touch log/.gitkeep
git ci -am "added log directory to git"

That's all, happy gitting!