我如何将一个空的目录(不包含文件)添加到一个Git存储库中?
当前回答
输入: 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承诺是令人惊讶的困难。
其他回答
安迪·莱斯特是对的,但如果你的目录只需要是空的,而不是空的,你可以把一个空的.gitignore 文件在那里作为一个工作室。
另一方面,这是一个实施问题,而不是一个基本的Git存储设计问题,正如在Git邮件列表上多次提到的那样,这一点没有实施的原因是没有人照顾足以提交一个插件,而不是它不能或不应该完成。
有时你需要处理错误的书面图书馆或软件,需要一个“真实”空白和现有目录。 设置一个简单的.gitignore 或.keep 可能会破坏它们,并导致一个错误。
首先,创建所需的目录:
mkdir empty
然后你将一个破碎的象征性链接添加到这个目录(但在任何其他情况下,除了上面描述的使用情况,请使用一个 README 与解释):
ln -s .this.directory empty/.keep
要忽略此目录中的文件,您可以将其添加到根.gitignore:
echo "/empty" >> .gitignore
要添加被忽略的文件,使用一个参数强制它:
git add -f empty/.keep
在承诺之后,你在你的指数中有一个破碎的象征性链接, git 创建了目录. 破碎的链接有某些好处,因为它不是正常的文件和没有正常的文件的点。
find empty -type f
$ php -r "var_export(glob('empty/.*'));"
array (
0 => 'empty/.',
1 => 'empty/..',
)
当您添加一个.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.
Jamie Flournoy的解决方案工作得很好. 这里有一个稍微改进的版本,以保持.htaccess :
# Ignore everything in this directory
*
# Except this file
!.gitignore
!.htaccess
使用此解决方案,您可以插入一个空的文件夹,例如 /log、 /tmp 或 /cache 并且文件夹将保持空白。
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的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日志日期格式