如何将空目录添加到 Git 存储库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/115983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 05:48:31  来源:igfitidea点击:

How can I add an empty directory to a Git repository?

gitdirectorygit-add

提问by Laurie Young

How can I add an empty directory (that contains no files) to a Git repository?

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

采纳答案by Jamie Flournoy

Another way to make a directory stay (almost) empty (in the repository) is to create a .gitignorefile inside that directory that contains these four lines:

另一种使目录(在存储库中)保持(几乎)为空的方法是.gitignore在该目录中创建一个包含以下四行的文件:

# Ignore everything in this directory
*
# Except this file
!.gitignore

Then you don't have to get the order right the way that you have to do in m104's solution.

那么您不必像在 m104 的解决方案中那样正确地获得订单。

This also gives the benefit that files in that directory won't show up as "untracked" when you do a git status.

这也有一个好处,即当您执行 git status 时,该目录中的文件不会显示为“未跟踪”。

Making @GreenAsJade's comment persistent:

使@GreenAsJade的评论持久化:

I think it's worth noting that this solution does precisely what the question asked for, but is not perhaps what many people looking at this question will have been looking for. This solution guarantees that the directory remains empty. It says "I truly never want files checked in here". As opposed to "I don't have any files to check in here, yet, but I need the directory here, files may be coming later".

我认为值得注意的是,这个解决方案完全满足了问题的要求,但可能不是很多看这个问题的人一直在寻找的。此解决方案可确保目录保持为空。它说“我真的不想在这里签入文件”。与“我在这里没有任何文件要签入,但我需要这里的目录,文件可能会稍后出现”相反。

回答by Andy Lester

You can't. See the Git FAQ.

你不能。请参阅Git 常见问题解答

Currently the design of the git index (staging area) only permits files to be listed, and nobody competent enough to make the change to allow empty directories has cared enough about this situation to remedy it.

Directories are added automatically when adding files inside them. That is, directories never have to be added to the repository, and are not tracked on their own.

You can say "git add <dir>" and it will add files in there.

If you really need a directory to exist in checkouts you should create a file in it. .gitignore works well for this purpose; you can leave it empty, or fill in the names of files you expect to show up in the directory.

目前 git 索引(暂存区)的设计只允许列出文件,没有人有足够的能力进行更改以允许空目录,已经足够关心这种情况来纠正它。

在目录中添加文件时会自动添加目录。也就是说,目录永远不必添加到存储库中,并且不会自行跟踪。

你可以说“ git add <dir>”,它会在那里添加文件。

如果您确实需要一个目录存在于结帐中,您应该在其中创建一个文件。.gitignore 很适合这个目的;您可以将其留空,或填写您希望出现在目录中的文件名。

回答by Artur79

Create an empty file called .gitkeepin the directory, and add that.

.gitkeep在目录中创建一个名为的空文件,并添加它。

回答by John Mee

You could always put a README file in the directory with an explanation of why you want this, otherwise empty, directory in the repository.

您总是可以在目录中放置一个 README 文件,并解释为什么要在存储库中使用这个目录,否则为空目录。

回答by Acumenus

touch .keep

On Linux, this creates an empty file named .keep. For what it's worth, this name is agnostic to Git, whereas .gitkeepwould be specific to Git. Secondly, as another user has noted, the .gitprefix convention should be reserved for files and directories that Git itself uses.

在 Linux 上,这会创建一个名为.keep. 就其价值而言,此名称与 Git 无关,而.gitkeep特定于 Git。其次,正如另一位用户所指出的,.git前缀约定应该保留给 Git 本身使用的文件和目录。

Alternatively, as noted in another answer, the directory can contain a descriptive READMEor README.mdfileinstead.

或者,如另一个答案中所述,目录可以包含描述性文件READMEREADME.md文件

Of course this requires that the presence of the file won't cause your application to break.

当然,这要求文件的存在不会导致您的应用程序中断。

回答by Cranio

Why would we need empty versioned folders

为什么我们需要空的版本文件夹

First things first:

第一件事:

An empty directory cannot be part of a tree under the Git versioning system.

在 Git 版本控制系统下,空目录不能成为树的一部分

It simply won't be tracked. But there are scenarios in which "versioning" empty directories can be meaningful, for example:

它根本不会被跟踪。但是在某些情况下,“版本控制”空目录可能是有意义的,例如:

  • scaffolding a predefined folder structure, making it available to every user/contributor of the repository; or, as a specialized case of the above, creating a folder for temporary files, such as a cache/or logs/directories, where we want to provide the folder but .gitignoreits contents
  • related to the above, some projects won't work without some folders(which is often a hint of a poorly designed project, but it's a frequent real-world scenario and maybe there could be, say, permission problems to be addressed).
  • 脚手架预定义的文件夹结构,使其可供存储库的每个用户/贡献者使用;或者,作为上述的特例,为临时文件创建一个文件夹,例如 acache/logs/目录,我们希望在其中提供文件夹但.gitignore其内容
  • 与上述相关,如果没有某些文件夹,某些项目将无法运行(这通常暗示项目设计不佳,但这是一个常见的现实世界场景,也许可能存在需要解决的权限问题)。

Some suggested workarounds

一些建议的解决方法

Many users suggest:

许多用户建议:

  1. Placing a READMEfile or another file with some content in order to make the directory non-empty, or
  2. Creating a .gitignorefile with a sort of "reverse logic" (i.e. to include all the files) which, at the end, serves the same purpose of approach #1.
  1. 放置README具有某些内容的文件或另一个文件以使目录非空,或
  2. 创建一个.gitignore具有某种“反向逻辑”(即包含所有文件)的文件,最终与方法 #1 的目的相同。

While both solutions surely workI find them inconsistent with a meaningful approach to Git versioning.

虽然这两种解决方案肯定都有效,但我发现它们与有意义的 Git 版本控制方法不一致。

  • Why are you supposed to put bogus files or READMEs that maybe you don't really want in your project?
  • Why use .gitignoreto do a thing (keepingfiles) that is the very opposite of what it's meant for (excludingfiles), even though it is possible?
  • 为什么你应该在你的项目中放置你可能并不真正想要的虚假文件或自述文件?
  • 为什么要使用.gitignore与它的意义(不包括文件)完全相反的事情(保存文件),即使它是可能的?

.gitkeep approach

.gitkeep 方法

Use an emptyfile called .gitkeepin order to force the presence of the folder in the versioning system.

使用一个名为的文件.gitkeep来强制版本控制系统中存在该文件夹。

Although it may seem not such a big difference:

虽然看起来差别不大:

  • You use a file that has the singlepurpose of keeping the folder. You don't put there any info you don't want to put.

    For instance, you should use READMEs as, well, READMEs with useful information, not as an excuse to keep the folder.

    Separation of concerns is always a good thing, and you can still add a .gitignoreto ignore unwanted files.

  • Naming it .gitkeepmakes it very clear and straightforward from the filename itself (and also to other developers, which is good for a shared project and one of the core purposes of a Git repository) that this file is

    • A file unrelated to the code (because of the leading dot and the name)
    • A file clearly related to Git
    • Its purpose (keep) is clearly stated and consistent and semantically opposed in its meaning to ignore
  • 您使用的文件的唯一目的是保留文件夹。你不会在那里放任何你不想放的信息。

    例如,您应该将 README 用作包含有用信息的 README,而不是作为保留文件夹的借口。

    关注点分离总是一件好事,你仍然可以添加一个.gitignore来忽略不需要的文件。

  • 命名它.gitkeep使文件名本身(以及其他开发人员,这对共享项目和 Git 存储库的核心目的之一有益)非常清楚和直接,该文件是

    • 与代码无关的文件(因为前导点和名称)
    • 一个与 Git 明确相关的文件
    • 它的目的 ( keep) 被清楚地陈述和一致并且语义上反对它的意思是忽略

Adoption

采用

I've seen the .gitkeepapproach adopted by very important frameworks like Laravel, Angular-CLI.

我已经看到LaravelAngular-CLI.gitkeep等非常重要的框架采用的方法。

回答by mjs

As described in other answers, Git is unable to represent empty directories in its staging area. (See the Git FAQ.) However, if, for your purposes, a directory is empty enough if it contains a .gitignorefile only, then you can create .gitignorefiles in empty directories only via:

如其他答案中所述,Git 无法在其暂存区域中表示空目录。(请参阅Git 常见问题解答。)但是,如果出于您的目的,如果一个.gitignore目录仅包含一个文件就足够空,那么您.gitignore只能通过以下方式在空目录中创建文件:

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

回答by Aristotle Pagaltzis

Andy Lester is right, but if your directory just needs to be empty, and not emptyempty, you can put an empty .gitignorefile in there as a workaround.

安迪·莱斯特是正确的,但如果你的目录只需要是空的,而不是空的,你可以把一个空.gitignore在那里文件作为一种变通方法。

As an aside, this is an implementation issue, not a fundamental Git storage design problem. As has been mentioned many times on the Git mailing list, the reason that this has not been implemented is that no one has cared enough to submit a patch for it, not that it couldn't or shouldn't be done.

顺便说一句,这是一个实现问题,而不是一个基本的 Git 存储设计问题。正如在 Git 邮件列表中多次提到的那样,没有实现这一点的原因是没有人足够关心为它提交补丁,而不是不能或不应该这样做。

回答by Thomas E

The Ruby on Railslog folder creation way:

Ruby on Rails的日志文件夹的创建方式:

mkdir log && touch log/.gitkeep && git add log/.gitkeep

Now the log directory will be included in the tree. It is super-useful when deploying, so you won't have to write a routine to make log directories.

现在日志目录将包含在树中。它在部署时非常有用,因此您不必编写例程来创建日志目录。

The logfiles can be kept out by issuing,

日志文件可以通过发出,

echo log/dev.log >> .gitignore

but you probably knew that.

但你可能知道。

回答by lesmana

Git does not track empty directories. See the Git FAQfor more explanation. The suggested workaround is to put a .gitignorefile in the empty directory. I do not like that solution, because the .gitignoreis "hidden" by Unix convention. Also there is no explanation why the directories are empty.

Git 不跟踪空目录。有关更多解释,请参阅Git 常见问题解答。建议的解决方法是将.gitignore文件放在空目录中。我不喜欢那个解决方案,因为它.gitignore被 Unix 约定“隐藏”了。也没有解释为什么目录是空的。

I suggest to put a README file in the empty directory explaining why the directory is empty and why it needs to be tracked in Git. With the README file in place, as far as Git is concerned, the directory is no longer empty.

我建议在空目录中放置一个 README 文件,解释为什么该目录为空以及为什么需要在 Git 中对其进行跟踪。有了 README 文件,就 Git 而言,目录不再是空的。

The real question is why do you need the empty directory in git? Usually you have some sort of build script that can create the empty directory before compiling/running. If not then make one. That is a far better solution than putting empty directories in git.

真正的问题是为什么你需要 git 中的空目录?通常,您有某种构建脚本可以在编译/运行之前创建空目录。如果没有,那就做一个。这是比在 git 中放置空目录更好的解决方案。

So you have some reason why you need an empty directory in git. Put that reason in the README file. That way other developers (and future you) know why the empty directory needs to be there. You will also know that you can remove the empty directory when the problem requiring the empty directory has been solved.

所以你有一些理由需要在 git 中创建一个空目录。把这个原因放在 README 文件中。这样其他开发人员(以及未来的你)就知道为什么需要空目录了。您还将知道,当需要空目录的问题已解决时,您可以删除空目录。



To list every empty directory use the following command:

要列出每个空目录,请使用以下命令:

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

To create placeholder READMEs in every empty directory:

在每个空目录中创建占位符自述文件:

find -name .git -prune -o -type d -empty -exec sh -c \
  "echo this directory needs to be empty because reasons > {}/README.emptydir" \;

To ignore everything in the directory except the README file put the following lines in your .gitignore:

要忽略目录中除 README 文件之外的所有内容,请将以下几行放入您的.gitignore

path/to/emptydir/*
!path/to/emptydir/README.emptydir
path/to/otheremptydir/*
!path/to/otheremptydir/README.emptydir

Alternatively, you could just exclude everyREADME file from being ignored:

或者,您可以只排除每个README 文件被忽略:

path/to/emptydir/*
path/to/otheremptydir/*
!README.emptydir

To list every README after they are already created:

在创建后列出每个 README:

find -name README.emptydir