我如何将一个空的目录(不包含文件)添加到一个Git存储库中?
当前回答
这里是一个黑客,但有趣的是它工作(Git 2.2.1) 类似于 @Teka 提出的,但更容易记住:
添加一个子模块到任何存储库(git 子模块添加 path_to_repo) 这将添加一个文件夹和一个文件.submodules. 执行一个更改. 删除.submodules 文件并执行更改。
现在,你有一个目录,当承诺被检查时创建。 有趣的是,如果你看看这个文件的木材对象的内容,你会得到:
致命: 没有有效的对象名称 b64338b90b4209263b50244d18278c0999867193
我不会鼓励使用它,因为它可能会停止在未来的版本的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
输入: https://gitlab.com/empty-repo/empty.git
一个 Git 存储库与正确的一个承诺:
commit e84d7b81f0033399e325b8037ed2b801a5c994e0
Author: Nobody <none>
Date: Thu Jan 1 00:00:00 1970 +0000
使用
要添加一个空的目录给你 GIT repo:
git submodule add https://gitlab.com/empty-repo/empty.git path/to/dir
将所有现有空白目录转换为子模块:
find . -type d -empty -delete -exec git submodule add -f https://gitlab.com/empty-repo/empty.git \{\} \;
Git 在创建子模块参考时将存储最新的 commit hash,所以你不必担心我(或GitLab)使用此注射恶意文件。 不幸的是,我没有找到任何方法来强制该 commit ID 在查询期间使用,所以你将不得不手动检查该参考 commit ID 是e84d7b81f0033399e325b8037ed2b801a5c994e0 使用 git 子模块状态添加后
附件:重新创建此承诺
您应该能够重新创建这个准确的承诺使用(在一个空的目录):
# Initialize new GIT repository
git init
# Set author data (don't set it as part of the `git commit` command or your default data will be stored as “commit author”)
git config --local user.name "Nobody"
git config --local user.email "none"
# Set both the commit and the author date to the start of the Unix epoch (this cannot be done using `git commit` directly)
export GIT_AUTHOR_DATE="Thu Jan 1 00:00:00 1970 +0000"
export GIT_COMMITTER_DATE="Thu Jan 1 00:00:00 1970 +0000"
# Add root commit
git commit --allow-empty --allow-empty-message --no-edit
创建可重复的Git承诺是令人惊讶的困难。
这个解决方案为我工作了。
1. 将.gitignore 文件添加到您的空白目录中:
*
*/
!.gitignore
* 忽略文件夹中的所有文件 */ 忽略子指南!.gitignore 包含.gitignore 文件
然后删除您的缓存,步行您的文件,承诺并按下:
git rm -r --cached .
git add . // or git stage .
git commit -m ".gitignore fix"
git push
您可以通过 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
也许添加一个空的目录似乎会是最少的抵抗的道路,因为你有脚本,希望该目录存在(也许是因为它是生成的二进制的目标)。
mkdir --parents .generated/bin ## create a folder for storing generated binaries
mv myprogram1 myprogram2 .generated/bin ## populate the directory as needed
在此例子中,您可以查看(破)标志性链接到目录,以便您可以访问它,而无需“创建”预定义(但这是可选的)。
ln -sf .generated/bin bin
git add bin
如果你想清理你的源树,你可以:
rm -rf .generated ## this should be in a "clean" script or in a makefile
如果您采取常见的检查方法,在一个几乎空的文件夹中,您有更少的复杂性,删除内容,而不删除“Gitignore”文件。
您可以忽略所有创建的文件,并将以下内容添加到您的 root.gitignore:
.generated
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的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日志日期格式