· Industry· 日本語で読む
How to Track Empty Directories in Git
A Zenn article explained how to configure Git to track an empty directory. The example uses a logs directory, places a dummy file in it and ignores other files inside. The key is to limit the rule to the target path and make only the dummy file an exception.
What happened
The Zenn article "[Git] Check In an Empty Directory" explained how to include an empty logs directory in Git while excluding files generated inside it. In the example, it places logs/.gitkeep in the directory, ignores /logs/* in .gitignore, then makes an exception for !/logs/.gitkeep.
Why it matters
Git tracks files, not directories on their own, so keeping a path requires combining a dummy file with ignore rules. The article's example shows how to track the top-level logs directory and its marker file while excluding generated files under logs.
Analysis
The key to this configuration is not to register an empty directory directly, but to keep the path by placing a file Git can track and exclude everything other than that file. In the article's example, logs/.gitkeep serves as a marker to keep the directory, while the ignore rule excludes files generated there from tracking. In other words, the configuration treats the directory's existence separately from whether its contents are version-controlled.
The pattern syntax determines the scope of this configuration. The article shows that the leading slash in /logs/* specifies the logs directory at the project root and targets files beneath it, while !/logs/.gitkeep limits the exception to that file. This differs from a rule that broadly targets any directory named logs or one that expands the scope of the exception. When a path such as a log output directory is needed but its generated files should not be included in the repository, it is important to be able to understand the intent of the rules at the path level.
The name .gitkeep is not a Git requirement, and the article says that any filename can be used if it is specified appropriately in .gitignore. Even when using the conventional name, the filename allowed for tracking must match the exception pattern. So when changing the name, it is also necessary to check that it remains consistent with the ignore rule. The method shown here is a configuration example for handling an empty directory; it does not suggest that all directories should be managed using the same rules. In practice, each repository must determine which paths to keep and which generated files to exclude. See the original article for the specific configuration example and further explanation.
Sources
Translated from the Japanese original: /news/zenn-git-de-sora-direkutori-wo-nokosu-setteihouhou-wo-shouka-151f96. The Japanese version is authoritative if they differ.