Git Ignore Everything Inside Directory Except the Directory Itself

June 23rd, 2021

Sometimes you may want to commit an empty directory into your git repo while ignoring all of its inner contents. For example, you may be creating a project boilerplate, and need to have a postgres-data directory that will map to your local docker container's volume and need to make sure that the direcotory is present when the repository is checked out. By default, git will not commit an empty direcotry into the repository. However, you can force git to commit an empty directory by adding a .gitkeep file indside that directory and editing your.gitignore file like so:

.gitgnore

postgres-data/*
!postgres-data/.gitkeep

First, we tell git to ignore all contents of /postgres-data. Then, we tell git to keep the .gitkeep file that we added to /postgres-data directory. As a result of this, git will commit the directory itself while ignore all of its inner contents with the exception of the .gitkeep file.

git keep 2

©2021 Michael Movsesov, All Rights Reserved