DEV Community

Cover image for How To Add An Empty Directory To A Git Repository — Two Different Use Cases

How To Add An Empty Directory To A Git Repository — Two Different Use Cases

talha131 profile image Talha Mansoor Originally published at on ・2 min read

Git lets you only add files to the index. If you want to add a directory, then Git does not have a builtin solution for you. Of course, there are ways to work around this issue.

Add a directory but ignore its contents

If you want to add an empty folder to Git index, but you want Git to omit the present and future contents of the directory. Then, create a .gitignore file inside the directory and add these lines to the .gitignore file:

# Disregard this entire folder
# Except this .gitignore file

Then add and commit this .gitignore file. This solution results in a directory that is present inside Git, but its contents never get added to the Git.

A possible use case scenario of this solution is to add the debug folder of a project to the index. Almost always, you do not want the contents of your debug folder to end up in the Git index. But you may need to keep the debug folder in the index because you have relative paths in your code or you want all your teammates to have the same folder structure.

Add an empty directory and track its future contents

Say, you want to add a folder to the index. It is currently blank, but you plan to add files to it that Git should track. The only solution is to create a file in the directory and add it to the Git.

Git community has settled on creating an empty file called .keep in the directory and adding it to the index.

mkdir hello
cd hello
touch .keep
git add .keep
git commit -m "add empty directory called hello"

.keep is not an officially prescribed name by the Git. You can use any other name. You can use "README" or "ABOUT", or even "Intentionally Blank File". Your choice. .keep is the most preferred filename for this purpose. The . is the beginning of its name, ensures that it will stay hidden on Linux and macOS file systems.

A possible use case scenario is if you are creating a boilerplate folder structure, which you want to reuse in all your React projects. You create empty folders for containers, components, API and store. You add these empty folders to the Git repository. You then use the repository as a template for your future projects.

Cover Image Attribution: James Best

Discussion (0)

Forem Open with the Forem app