Git: Don’t create .gitkeep files, use .gitignore instead - Adam Johnson
Hacker News
February 20, 2026
AI-Generated Deep Dive Summary
Git repositories only track files, not empty directories, which can cause issues when trying to ensure specific directories exist in cloned repositories. For example, a directory like "build" might be needed for output but won't be created by Git unless it contains tracked files.
Traditionally, developers have used an empty file named `.gitkeep` within the target directory to signal Git to track the directory. This method involves adding the `.gitkeep` file and updating the `.gitignore` file to exclude all other files in the directory except for `.gitkeep`. While effective, this approach has drawbacks: it requires modifying two files, can lead to maintenance issues if directories are renamed, and may confuse developers unfamiliar with the non-standard `.gitkeep` naming convention.
A more efficient solution is to use only a `.gitignore` file within the directory. By creating a `.gitignore` file containing `*` followed by an exception for itself (`!.gitignore`), Git will track this file while ignoring others, ensuring the directory's existence without needing an empty file. This method is simpler, avoids potential issues with directory renaming, and leverages standard Git practices.
This approach matters because maintaining consistent directories across clones ensures reliability in development environments, especially in team settings or during deployments. Using `.git
Verticals
techstartups
Originally published on Hacker News on 2/20/2026