如何向 Git 里添加空目录

Git only tracks files, not directories. It will only create a directory if it contains a tracked file. But sometimes you need to “track” a directory, to ensure it exists for fresh clones of a repository.

假设我们想在 git 仓库里保留一个 database 空目录,

Old Way

database/
├── README.md
└── database.sqlite  (应用执行时自动创建)

在项目根 .gitignore 里这样写

/database/*
!/database/README.md

Smart Way

database/
└── .gitignore

在此子目录 .gitignore 里写

*
!.gitignore
发表于 2026 年 3 月 10 日,星期二
更新于 2026 年 5 月 4 日,星期一