我如何将一个空的目录(不包含文件)添加到一个Git存储库中?
当前回答
正如其他答案中所描述的那样,Git 无法在其排序区域中代表空的目录(见 Git FAQ ) 但是,如果为您的目的,一个目录是空的,如果它只包含一个.gitignore 文件,那么您只能通过:
find . -type d -empty -exec touch {}/.gitignore \;
其他回答
您可以通过 create_readme.php 保存此代码并从您的 Git 项目的根目录运行 PHP 代码。
php create_readme.php
它将添加 README 文件到所有是空的目录,因此这些目录将被添加到指数。
<?php
$path = realpath('.');
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $name => $object){
if ( is_dir($name) && ! is_empty_folder($name) ){
echo "$name\n" ;
exec("touch ".$name."/"."README");
}
}
function is_empty_folder($folder) {
$files = opendir($folder);
while ($file = readdir($files)) {
if ($file != '.' && $file != '..')
return true; // Not empty
}
}
?>
然后做
git commit -m "message"
git push
将.gitkeep 文件添加到空的目录中,并承诺。
touch .gitkeep
这是Git跟随的标准。
假设你需要一个空的目录,称为tmp:
$ mkdir tmp
$ touch tmp/.gitignore
$ git add tmp
$ echo '*' > tmp/.gitignore
$ git commit -m 'Empty directory' tmp
换句话说,你需要将.gitignore 文件添加到指数之前,你可以告诉 Git 忽略它(以及所有其他在空的目录中)。
只需添加一个 readme 或.gitignore 文件,然后从 GitHub 网站删除它,但不是终端。
只需在您想要跟踪的空白目录中添加一个空白(没有内容).gitignore 文件。
例如,如果你想跟踪一个空的目录, /项目/内容/帖子,然后创建一个新的空的文件, /项目/内容/帖子/.gitignore
注意:.gitkeep 不是官方 Git 的部分:
此分類上一篇
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- 如何复制在bash所有目录和文件递归?
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式