我如何将一个空的目录(不包含文件)添加到一个Git存储库中?
当前回答
我正在寻找这个问题,因为:我创建一个新的目录,它包含许多文件。在这些文件中,一些我想添加到Git存储库,有些不。
Untracked files:
(use "git add <file>..." to include in what will be committed)
../trine/device_android/
它不在这个新目录中列出单独的文件,然后我认为也许我只能添加这个目录,然后处理单独的文件,所以我谷歌“只添加目录”。
在我的情况下,我发现我只能在新目录中添加一个文件,我确信我想将其添加到Git。
git add new_folder/some_file
此后,“Git状态”将显示单独文件的状态。
其他回答
您可以通过 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的解决方案工作得很好. 这里有一个稍微改进的版本,以保持.htaccess :
# Ignore everything in this directory
*
# Except this file
!.gitignore
!.htaccess
使用此解决方案,您可以插入一个空的文件夹,例如 /log、 /tmp 或 /cache 并且文件夹将保持空白。
安迪·莱斯特是对的,但如果你的目录只需要是空的,而不是空的,你可以把一个空的.gitignore 文件在那里作为一个工作室。
另一方面,这是一个实施问题,而不是一个基本的Git存储设计问题,正如在Git邮件列表上多次提到的那样,这一点没有实施的原因是没有人照顾足以提交一个插件,而不是它不能或不应该完成。
当您添加一个.gitignore 文件时,如果您将放置任何内容(您希望 Git 忽略),您可能希望添加一个单一行,只需一个星级 * 以确保您不会随机添加被忽略的内容。
我喜欢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日志日期格式