Empty directory on Git: Yes, It's possible

Often the folder hierarchy is important in our projects and we need to create empty folders in the repository.

Many developers eventually discover the problem when they deploy to the server and notice that the folders have not been added to the commit.

How to create empty directories then? By default, git only cares about files, and when there is a directory without files, it is ignored in version control. But there are some solutions to the problem.

The solution to this problem is to create an almost empty folder.

Creating an almost empty directory

After creating your empty folder, create a .gitignore file inside it, so it can be added to the commit and be part of the repository. In fact, any file can be used for that matter. Create a hidden .keep file if you prefer, or even a README.

The advantage of gitignore is that it's already a standard way to do this and it can also be used to ignore other files.

The hidden .gitignore file can be empty or contain a list of files you want to ignore in your commits later.

Let's say you want to create an uploads folder and add only this folder to the repository, but that all future files inside of it are ignored.

Create a .gitignore file inside the folder and add the following content:

# Exclude all files from being added to the repository
*
# Except itself
!.gitignore

This way, the folder will be created only with the .gitignore and no other file added to it in the future will be considered by git.

If you want other files to be considered in the future, just create an empty .gitignore file.

Did you find this helpful?

Ricardo Metring

Ricardo Metring

Full stack web developer and co-founder at Criar.io.
I've worked for over 10 years with software development. I'm constantly curious and learning.

Linkedin     Github

Related articles