Empty Directories In Git

Git LogoOccasionally when working on a code base the application at hand requires a directory for the storage of log files, uploads etc. You often like to keep the directory itself in the repository but you don’t want any of its contents that have a temporary lifespan making their way into the project git repository.

Which causes a dilemma git will not keep directories with no files inside them within your repository. But leaving the directory out of the repository normally causes its own set of issues. For example if another developer clones the codebase they will most likely be faced with random application errors due to your project missing all of its required directories.

Documentation can always be created to inform people what directories need to be created and where. But in a lot of situations its possible the supporting documentation may get overlooked or simply ignored. To get around this I will show you how to create an empty directory in a git repository using a bit of trickery with a .gitignore file.

To get started simply create the new directory you would like to create with the project code base. Then create a file inside the directory by the name of .gitignore that contains the following:

[codesyntax lang=”bash”]

# Ignore the contents of this directory
*
!.gitignore

[/codesyntax]

This tells git to ignore everything in the directory bar the .gitignore file itself. After the file is added to the repository it will allow you to keep the directory with the rest of the application code but not have to worry about any files it contains being added by accident as well.