Skip to content

Lallethu/autogitkeep

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

Auto .gitkeep

Overview

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.


How It Works

Attention

Please, make sure to move the script at the root level of your project!

  1. The script starts from the current working directory (getcwd()).

  2. It scans each directory using scandir().

  3. It filters out unwanted entries such as:

    • . (current directory)
    • .. (parent directory)
    • .git (repository metadata)
  4. If a directory is empty after filtering, a .gitkeep file is created.

  5. If not empty, the script recursively processes subdirectories.


Usage

Modify $excludesDirectories (if needed)

['.', '..', '.git']

Defines directories that should be ignored.

You can extend this list to exclude additional folders such as:

  • node_modules
  • vendor
  • cache

Run from CLI

php autogitkeep.php

The script will process all directories starting from where it is executed.


Example

Before

project/
├── main.php
├── src/
├── storage/        (empty)
└── assets/         (empty)

After

project/
├── main.php
├── src/
├── storage/
│   └── .gitkeep
└── assets/
    └── .gitkeep

Notes

  • The script does not overwrite existing .gitkeep files.
  • Only truly empty directories (after filtering) are affected.
  • Works on Windows, Linux, and macOS thanks to DIRECTORY_SEPARATOR.

Best Practices

  • 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.

License

Free to use and modify.

About

PHP Script that add .gitkeep to empty folder

Topics

Resources

Stars

Watchers

Forks

Contributors

Languages