我如何将一个空的目录(不包含文件)添加到一个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承诺是令人惊讶的困难。

其他回答

我正在寻找这个问题,因为:我创建一个新的目录,它包含许多文件。在这些文件中,一些我想添加到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状态”将显示单独文件的状态。

也许添加一个空的目录似乎会是最少的抵抗的道路,因为你有脚本,希望该目录存在(也许是因为它是生成的二进制的目标)。

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

只需在您想要跟踪的空白目录中添加一个空白(没有内容).gitignore 文件。

例如,如果你想跟踪一个空的目录, /项目/内容/帖子,然后创建一个新的空的文件, /项目/内容/帖子/.gitignore

注意:.gitkeep 不是官方 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.

你不能,不幸的是,你永远不会能做到这一点,这是Linus Torvald自己做的决定,他知道什么对我们有好处。

这里有一个地方,我读过一次。

我找到了 Re: 空的目录,但也许还有另一个。

你必须和工人一起生活......不幸的是。