Skip to main content
Chapter 32 of 61 · 1 sections · Page 133

Git: Empty Directories

Work around Git’s file-only tracking model using .gitkeep and directory conventions.

Original chapter reference: Chapter 32: Empty directories in Git. The command options and documentation recipes below are fully transcribed from Git Notes for Professionals.

Section 32.1: Git doesn't track directories

Assume you've initialized a project with the following directory structure:

Source example · PDF page 133
/build
app.js

Then you add everything so you've created so far and commit:

Source example · PDF page 133
git init
git add .
git commit -m "Initial commit"

Git will only track the file app.js.

Assume you added a build step to your application and rely on the "build" directory to be there as the output directory (and you don't want to make it a setup instruction every developer has to follow), a convention is to include a ".gitkeep" file inside the directory and let Git track that file.

Source example · PDF page 133
/build
  .gitkeep
app.js

Then add this new file:

Source example · PDF page 133
git add build/.gitkeep
git commit -m "Keep the build directory around"

Git will now track the file build/.gitkeep file and therefore the build folder will be made available on checkout.

Again, this is just a convention and not a Git feature.

Buy me a coffee
Daily Practice
0/0