我如何将一个空的目录(不包含文件)添加到一个Git存储库中?
当前回答
正如其他答案中所描述的那样,Git 无法在其排序区域中代表空的目录(见 Git FAQ ) 但是,如果为您的目的,一个目录是空的,如果它只包含一个.gitignore 文件,那么您只能通过:
find . -type d -empty -exec touch {}/.gitignore \;
其他回答
也许添加一个空的目录似乎会是最少的抵抗的道路,因为你有脚本,希望该目录存在(也许是因为它是生成的二进制的目标)。
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。
/app/data/**/*!/app/data/**/*.md
然后你可以承诺描述的 README.md 文件(或白色文件,不管,只要你可以针对它们独特的类似于 *.md 在这种情况下)在每个目录,以确保所有目录仍然是 repo 的部分,但文件(有扩展)被忽略。
您可以用 xml / 图像文件或任何东西填写所有这些目录,并随着时间的推移在 /app / data / 下添加更多目录,因为您的应用程序开发的存储需要(使用 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承诺是令人惊讶的困难。
只需添加一个 readme 或.gitignore 文件,然后从 GitHub 网站删除它,但不是终端。
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}