Physical AI News (Japan)

Physical AI, robotics and compute — reported from Japan.

日本語
· Industry· 日本語で読む

How to Keep Empty Directories in Git: Creating .gitkeep Files

Git explains that because it tracks files individually, empty directories are not tracked. It introduces a method of creating a “.gitkeep” file inside a directory. For settings that ignore dotfiles with “.*,” it says to add “!.gitkeep.”

Key facts

  • The gist explains that Git tracks files individually, so empty directories are not tracked.
  • It introduces a method of creating a “.gitkeep” file inside an empty directory.
  • For `.gitignore` settings that ignore “.*,” it gives `!.gitkeep` as an example of an exception.

What happened

A gist describes how to keep empty directories in Git by creating a “.gitkeep” file inside each directory. It shows a command to find empty directories and create a file in each one, along with an example `.gitignore` setting that exempts “.gitkeep” from being ignored.

Why it matters

For those who want to keep empty directories in Git, the gist shows how to place a “.gitkeep” file in them. It also says an exception is needed when `.gitignore` is set to ignore dotfiles, covering both file creation and ignore rules.

Analysis

The gist addresses an operational issue: Git tracks files, so empty directories cannot be kept as they are. The proposed solution is to place a “.gitkeep” file in each empty directory and use the file to represent the state of the working tree, including the directory. The key point is that the file is described not as having a special function, but as a marker for an otherwise empty location. The steps have two parts. First, use `find` to locate empty directories and create files in them. Then, if `.gitignore` is configured to ignore files whose names begin with a dot, add `!.gitkeep` as an exception to the ignore rule. Therefore, creating the file alone is not enough; the ignore settings must also be checked. The command shown excludes paths matching `./.git*` from the search and processes empty directories across the working tree. The method treats empty directories through files placed inside them, rather than tracking the directories themselves. If a team wants to maintain the same structure, including `.gitkeep` files in its workflow is one option. However, the gist presents this method as “apparently a good idea” and does not explain whether `.gitkeep` has any special meaning or which projects should adopt it. The material also does not establish whether the filename or workflow needs to be standardized. What remains to be checked is how ignore rules apply in the actual repository and whether a marker file is needed for the purpose of keeping empty directories. The gist does not discuss use by multiple people, precedence among existing ignore settings, or how to verify the result after creating the files. Those using the command should therefore check the target directories and `.gitignore` settings individually.

Details

For finding empty directories and creating files, the gist gives the command `find . -type d -empty -not -path './.git*' -exec touch {}\/\.gitkeep \;`. It also shows how to add `!.gitkeep` when `.gitignore` contains `*~` and `.*`, so that “.gitkeep” is not ignored.

Sources

Translated from the Japanese original: /news/git-de-sora-direkutori-wo-nokosu-houhou-gitkeep-no-sakusei-w-1c6cdf. The Japanese version is authoritative if they differ.