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:


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


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

Like what you read? Give Saeed Izadi a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.