Sub topic 12
Keeping empty directories with .gitkeep
This chapter assumes you know the basic Git workflow (git add, git commit, git status) and how .gitignore negation with ! works. The previous chapters covered both.
Git tracks files, not directories
Git stores file contents and the paths that lead to them. It has no record type for a directory on its own. If a directory holds no files, git add has nothing to stage, and the directory never appears in a commit.
This is a problem when your project expects a directory to exist. A typical case is a logs/ or uploads/ directory that the application writes into at runtime. The contents should be ignored, but a fresh clone still needs the directory.
mkdir -p logs
git statusgit status reports a clean tree, because the empty logs/ directory is invisible to Git.
The placeholder file
The standard fix is to put a small file in the directory so Git has something to track. By convention the file is named .gitkeep.
mkdir -p logs
touch logs/.gitkeep
git add logs/.gitkeep
git commit -m "Add logs directory"Git gives .gitkeep no special treatment. The name is only a convention, and any file would work. The leading dot hides it from casual directory listings, and the name tells other developers why the file exists. Some projects use .keep or a README instead.
Combining the placeholder with ignore rules
Usually you want the directory to exist but its contents to stay out of version control. That is where negation comes in. Ignore everything inside the directory, then re-include the placeholder.
logs/*
!logs/.gitkeepThe pattern is logs/*, not logs/. If you ignored the directory itself, Git would never look inside it, and the ! line would have no effect. Ignoring the contents individually leaves the directory open to inspection.
Check that the rules do what you expect:
touch logs/app.log
git status --short
git check-ignore -v logs/app.log logs/.gitkeepgit status should list logs/.gitkeep as a candidate to add, and it should not list app.log. The check-ignore command prints a match for app.log. For .gitkeep it prints a line starting with the negated pattern, because -v also reports negated matches.
A self-contained alternative
Instead of editing the root .gitignore, you can put a .gitignore inside the directory itself. It ignores everything except itself.
*
!.gitignoreSave this as logs/.gitignore and commit it. The file is tracked, so the directory exists in every clone, and every other file in the directory is ignored. This keeps the rule next to the directory it protects, so it moves with the directory if you rename or relocate it.
| Approach | Where the rule lives | Good fit |
|---|---|---|
| `.gitkeep` plus root rules | Root `.gitignore` | Several placeholder directories managed in one place |
| Nested `.gitignore` | Inside the directory | One self-describing directory with no root changes |
| `.gitkeep` only | Nowhere | Directory whose contents you do want to commit |
Common mistakes
- Ignoring the directory with a trailing slash.
logs/followed by!logs/.gitkeepfails. Uselogs/*instead. - Forgetting the placeholder is itself matched by
*. In a nested.gitignore, the!.gitignoreline is required. Without it, the file ignores itself and the directory disappears from new clones. - Expecting
.gitkeepto be built in. Git ignores the name. If the file is deleted, the directory goes with it. - Creating the placeholder after the files exist. This works, but tracked files are not affected by new ignore rules. Review the earlier chapter on files Git already tracks if
git statusstill shows contents you meant to ignore.
Exercise
Your project needs an uploads/ directory to exist after cloning, but uploaded files must never be committed. Write the root .gitignore lines and name the one file you must create and commit. Why would uploads/ followed by !uploads/.gitkeep fail?
Show answer
Add uploads/* and !uploads/.gitkeep to .gitignore, then create and commit uploads/.gitkeep. The uploads/ form ignores the directory itself, so Git never looks inside it and cannot apply the negation to .gitkeep. The placeholder stays ignored, the directory is not tracked, and it is missing from fresh clones.