我如何将一个空的目录(不包含文件)添加到一个Git存储库中?
当前回答
要将 Jamie Flournoy 的解决方案扩展到一个目录树上,您可以将此.gitignore 文件放在顶级目录中,并在 Git 应该跟踪的每个子目录中点击.keepdir。
# Ignore files but not directories. * matches both files and directories
# but */ matches only directories. Both match at every directory level
# at or below this one.
*
!*/
# Git doesn't track empty directories, so track .keepdir files, which also
# tracks the containing directory.
!.keepdir
# Keep this file and the explanation of how this works
!.gitignore
!Readme.md
其他回答
如上所述,无法添加空的目录,但这里是一个单一的列,将空的.gitignore 文件添加到所有目录中。
ruby -e'require "fileutils" ; Dir.glob(["target_directory","target_directory/**"]).each {♰f♰ FileUtils.touch(File.join(f, ".gitignore")) 如果 File.directory?(f) }'
我把它放在一个Rakefile中,以便轻松地访问。
另一种方式让一个目录保持(几乎)空白(在存储库中)是创建一个.gitignore 文件在该目录内,其中包含以下四行:
# Ignore everything in this directory
*
# Except this file
!.gitignore
然后你不需要得到订单正确的方式,你必须在m104的解决方案。
此外,这还提供了该目录中的文件不会在您进行 git 状态时显示为“不受吸引”的好处。
让 @GreenAsJade的评论持久:
我认为值得注意的是,这个解决方案正是问题所提出的,但并不确定许多人正在寻找什么。这个解决方案保证目录仍然是空的. 它说“我真的从来不想要文件在这里检查”。 相反,“我没有任何文件在这里检查,但是,我需要目录在这里,文件可能会出现。
您可以通过 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
要将 Jamie Flournoy 的解决方案扩展到一个目录树上,您可以将此.gitignore 文件放在顶级目录中,并在 Git 应该跟踪的每个子目录中点击.keepdir。
# Ignore files but not directories. * matches both files and directories
# but */ matches only directories. Both match at every directory level
# at or below this one.
*
!*/
# Git doesn't track empty directories, so track .keepdir files, which also
# tracks the containing directory.
!.keepdir
# Keep this file and the explanation of how this works
!.gitignore
!Readme.md
但我建议:让一个构建脚本或部署脚本在网站上创建目录。
潜在问题:目录不再空。
现在,而不是试图得到 git 跟踪空的目录 允许探索其他选项. 也许(希望)你有一个部署脚本. 让部署脚本创建目录后 git 克隆. 或者你有一个构建脚本. 让构建脚本创建目录后编辑. 或者也许甚至修改工具本身检查并创建目录之前使用。
我认为这是对问题的更敏感的方法. 构建脚本和部署脚本是为了准备事物运行程序。 您的工具需要一个空的目录. 因此,使用这些脚本来创建空的目录。
独自行走是危险的,请遵循这些命令。
find -type d -empty
find -name .git -prune -o -type d -empty -print
下列命令可能会帮助您,如果您继承了一个包含“空白”目录的项目。
列出包含一个名为.gitkeep 的文件的每个目录:
find -type f -name .gitkeep
find -type f -printf "%h\n" | sort | uniq -c | sort -n
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的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日志日期格式