How to use gitignore to not ignore
I was recently working on a problem where I needed a git repository to have a folder that is empty. The reason for this was that the folder structure needed to be maintained such that when the repository was loaded into a new system, then dependent applications can easily place files within those already created folders.
Now, the standard way that repository folder structures work is that folders are not tracked if there are not any files within those folders.
More specifically, I want the system to ignore all files within the folder, but to still make the folder when I clone the repository.
Now, this solution doesn’t really circumvent the problem of having to have files within the folders in order to keep the folder tracked, however, it can be seen as empty.
This simple method is to place a .gitignore
file in the folder, then put the following code in the file (code found here):
# Ignore everything in this directory
*
# Except this file
!.gitignore
I also found this handy command in another answer for adding a .gitignore
file in each of the empty folders within the repository:
find . -name .git -prune -o -type d -empty -exec touch {}/.gitignore \;
It’s also obviously had much discussion and is often needed based on the fact that it is one of the FAQ’s on the git website.