HowTo track a folder and not its content in Git

It may happen that you would like to track folders/directories in your git but not their content. As happened for me, I would always like to store my data and (learned) models in separate directories but ignore their (often large) files. However, I prefer to have these empty folders in my repository.

You may jump to the solution of adding some stuff in the .gitignore file placed in your root, but, as you may know, git tracks files and not folders! As a result, the following solutions does NOT work:

<my_directory>/*.*
<my_directory>/

The best solution I came across is to create a .gitignore file in the target directory and put the following lines in it:

*
*/
!.gitignore

Now, the empty folders are always available in your repository without their contents.

Show your support

Clapping shows how much you appreciated Saeed Izadi’s story.