我如何将一个空的目录(不包含文件)添加到一个Git存储库中?
当前回答
您可以! 查看 Git FAQ。
目前,Git指数的设计仅允许文件列出,并且没有人有足够的能力进行更改,以便空的目录对这种情况有足够的关注,以便解决它。
其他回答
这里是一个黑客,但有趣的是它工作(Git 2.2.1) 类似于 @Teka 提出的,但更容易记住:
添加一个子模块到任何存储库(git 子模块添加 path_to_repo) 这将添加一个文件夹和一个文件.submodules. 执行一个更改. 删除.submodules 文件并执行更改。
现在,你有一个目录,当承诺被检查时创建。 有趣的是,如果你看看这个文件的木材对象的内容,你会得到:
致命: 没有有效的对象名称 b64338b90b4209263b50244d18278c0999867193
我不会鼓励使用它,因为它可能会停止在未来的版本的Git,这可能会让您的存储库腐败。
当您添加一个.gitignore 文件时,如果您将放置任何内容(您希望 Git 忽略),您可能希望添加一个单一行,只需一个星级 * 以确保您不会随机添加被忽略的内容。
Jamie Flournoy的解决方案工作得很好. 这里有一个稍微改进的版本,以保持.htaccess :
# Ignore everything in this directory
*
# Except this file
!.gitignore
!.htaccess
使用此解决方案,您可以插入一个空的文件夹,例如 /log、 /tmp 或 /cache 并且文件夹将保持空白。
您可以通过 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
我喜欢Artur79和mjs的答案,所以我已经使用了两者的组合,并为我们的项目创造了标准。
find . -type d -empty -exec touch {}/.gitkeep \;
但是,只有少数我们的开发人员在Mac或Linux上工作,很多工作在Windows上,我找不到一个相当的简单的单线来完成相同的。
因此,由于我们大多数开发人员已经安装了Ant,我想到的第一件事是将一个Ant构建文件组合起来,以便独立于平台完成这一点。
pip3 install gitkeep2
它将允许你创建和删除.gitkeep 文件重复,它也将允许你添加消息给他们,让你的同事了解为什么这些目录是重要的。
$ gitkeep --help
Usage: gitkeep [OPTIONS] PATH
Add a .gitkeep file to a directory in order to push them into a Git repo
even if they're empty.
Read more about why this is necessary at: https://git.wiki.kernel.org/inde
x.php/Git_FAQ#Can_I_add_empty_directories.3F
Options:
-r, --recursive Add or remove the .gitkeep files recursively for all
sub-directories in the specified path.
-l, --let-go Remove the .gitkeep files from the specified path.
-e, --empty Create empty .gitkeep files. This will ignore any
message provided
-m, --message TEXT A message to be included in the .gitkeep file, ideally
used to explain why it's important to push the specified
directory to source control even if it's empty.
-v, --verbose Print out everything.
--help Show this message and exit.
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的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日志日期格式