This script recursively scans directories starting from the current working directory and automatically creates a .gitkeep file inside every empty folder.
Git does not track empty directories by default. The .gitkeep file is a widely used convention to force Git to include those directories in version control.
Please, make sure to move the script at the root level of your project!
-
The script starts from the current working directory (
getcwd()). -
It scans each directory using
scandir(). -
It filters out unwanted entries such as:
.(current directory)..(parent directory).git(repository metadata)
-
If a directory is empty after filtering, a
.gitkeepfile is created. -
If not empty, the script recursively processes subdirectories.
['.', '..', '.git']Defines directories that should be ignored.
You can extend this list to exclude additional folders such as:
node_modulesvendorcache
php autogitkeep.phpThe script will process all directories starting from where it is executed.
project/
├── main.php
├── src/
├── storage/ (empty)
└── assets/ (empty)project/
├── main.php
├── src/
├── storage/
│ └── .gitkeep
└── assets/
└── .gitkeep- The script does not overwrite existing
.gitkeepfiles. - Only truly empty directories (after filtering) are affected.
- Works on Windows, Linux, and macOS thanks to
DIRECTORY_SEPARATOR.
- Keep the exclusion list updated for your project structure.
- Run this script before committing if your project relies on empty directories.
- Consider adding it as part of your tooling or automation workflow.
Free to use and modify.