我如何将一个空的目录(不包含文件)添加到一个Git存储库中?


当前回答

您可以随时将 README 文件放入目录中,并解释为什么您想要此,否则空白,目录在存储库中。

其他回答

正如其他答案中所描述的那样,Git 无法在其排序区域中代表空的目录(见 Git FAQ ) 但是,如果为您的目的,一个目录是空的,如果它只包含一个.gitignore 文件,那么您只能通过:

find . -type d -empty -exec touch {}/.gitignore \;

输入: 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承诺是令人惊讶的困难。

没有办法 Git 跟踪目录,所以唯一的解决方案是在您想要 Git 跟踪的目录中添加一个位置持有者文件。

文件可以被命名并包含你想要的任何东西,但大多数人使用一个空白的文件称为.gitkeep (尽管有些人更喜欢VCS诊断的.keep)。

预定. 标记它作为一个隐藏的文件。

另一个想法是添加一个 README 文件,解释该目录将用于什么。

当您添加一个.gitignore 文件时,如果您将放置任何内容(您希望 Git 忽略),您可能希望添加一个单一行,只需一个星级 * 以确保您不会随机添加被忽略的内容。

只需添加一个 readme 或.gitignore 文件,然后从 GitHub 网站删除它,但不是终端。