我如何将一个空的目录(不包含文件)添加到一个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.
其他回答
正如其他答案中所描述的那样,Git 无法在其排序区域中代表空的目录(见 Git FAQ ) 但是,如果为您的目的,一个目录是空的,如果它只包含一个.gitignore 文件,那么您只能通过:
find . -type d -empty -exec touch {}/.gitignore \;
您可以随时将 README 文件放入目录中,并解释为什么您想要此,否则空白,目录在存储库中。
你不能,不幸的是,你永远不会能做到这一点,这是Linus Torvald自己做的决定,他知道什么对我们有好处。
这里有一个地方,我读过一次。
我找到了 Re: 空的目录,但也许还有另一个。
你必须和工人一起生活......不幸的是。
这里是一个黑客,但有趣的是它工作(Git 2.2.1) 类似于 @Teka 提出的,但更容易记住:
添加一个子模块到任何存储库(git 子模块添加 path_to_repo) 这将添加一个文件夹和一个文件.submodules. 执行一个更改. 删除.submodules 文件并执行更改。
现在,你有一个目录,当承诺被检查时创建。 有趣的是,如果你看看这个文件的木材对象的内容,你会得到:
致命: 没有有效的对象名称 b64338b90b4209263b50244d18278c0999867193
我不会鼓励使用它,因为它可能会停止在未来的版本的Git,这可能会让您的存储库腐败。
要将 Jamie Flournoy 的解决方案扩展到一个目录树上,您可以将此.gitignore 文件放在顶级目录中,并在 Git 应该跟踪的每个子目录中点击.keepdir。
# Ignore files but not directories. * matches both files and directories
# but */ matches only directories. Both match at every directory level
# at or below this one.
*
!*/
# Git doesn't track empty directories, so track .keepdir files, which also
# tracks the containing directory.
!.keepdir
# Keep this file and the explanation of how this works
!.gitignore
!Readme.md
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的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日志日期格式