我如何将一个空的目录(不包含文件)添加到一个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 FAQ。

目前,Git指数的设计仅允许文件列出,并且没有人有足够的能力进行更改,以便空的目录对这种情况有足够的关注,以便解决它。

如上所述,无法添加空的目录,但这里是一个单一的列,将空的.gitignore 文件添加到所有目录中。

ruby -e'require "fileutils" ; Dir.glob(["target_directory","target_directory/**"]).each {♰f♰ FileUtils.touch(File.join(f, ".gitignore")) 如果 File.directory?(f) }'

我把它放在一个Rakefile中,以便轻松地访问。

但我建议:让一个构建脚本或部署脚本在网站上创建目录。



潜在问题:目录不再空。


现在,而不是试图得到 git 跟踪空的目录 允许探索其他选项. 也许(希望)你有一个部署脚本. 让部署脚本创建目录后 git 克隆. 或者你有一个构建脚本. 让构建脚本创建目录后编辑. 或者也许甚至修改工具本身检查并创建目录之前使用。

我认为这是对问题的更敏感的方法. 构建脚本和部署脚本是为了准备事物运行程序。 您的工具需要一个空的目录. 因此,使用这些脚本来创建空的目录。


独自行走是危险的,请遵循这些命令。

find -type d -empty

find -name .git -prune -o -type d -empty -print

下列命令可能会帮助您,如果您继承了一个包含“空白”目录的项目。

列出包含一个名为.gitkeep 的文件的每个目录:

find -type f -name .gitkeep

find -type f -printf "%h\n" | sort | uniq -c | sort -n

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

PowerShell 版本:

在目录中找到所有空的文件夹 添加一个空的.gitkeep 文件在那里

Get-ChildItem 'Path to your Folder' -Recurse -Directory | Where-Object {[System.IO.Directory]::GetFileSystemEntries($_.FullName).Count -eq 0} | ForEach-Object { New-Item ($_.FullName + "\.gitkeep") -ItemType file}