git 如何 .gitignore 文件夹中的所有文件/文件夹,而不是文件夹本身?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4250063/
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 09:31:28  来源:igfitidea点击:

How to .gitignore all files/folder in a folder, but not the folder itself?

gitfiledirectorygitignore

提问by Aron Woost

Possible Duplicate:
How do I add an empty directory to a git repository

可能的重复:
如何将空目录添加到 git 存储库

I want to check in a blank folder. How can I do this?

我想签入一个空白文件夹。我怎样才能做到这一点?

采纳答案by kubi

You can't commit empty folders in git. If you want it to show up, you need to put something in it, even just an empty file.

你不能在 git 中提交空文件夹。如果你想让它显示出来,你需要在里面放一些东西,即使只是一个空文件。

For example, add an empty file called .gitkeep to the folder you want to keep, then in your .gitignore file write:

例如,将一个名为 .gitkeep 的空文件添加到您要保留的文件夹中,然后在您的 .gitignore 文件中写入:

# exclude everything
somefolder/*

# exception to the rule
!somefolder/.gitkeep 

Commit your .gitignore and .gitkeep files and this should resolve your issue.

提交您的 .gitignore 和 .gitkeep 文件,这应该可以解决您的问题。

回答by Trianam

put this .gitignore into the folder, then git add .gitignore

将此 .gitignore 放入文件夹中,然后 git add .gitignore

*
*/
!.gitignore

The *line tells git to ignore all files in the folder, but !.gitignoretells git to still include the .gitignore file. This way, your local repository and any other clones of the repository all get both the empty folder and the .gitignore it needs.

*行告诉 git 忽略文件夹中的所有文件,但!.gitignore告诉 git 仍然包含 .gitignore 文件。这样,您的本地存储库和存储库的任何其他克隆都会获得空文件夹和它需要的 .gitignore。

Edit: May be obvious but also add */ to the git ignore to also ignore sub folders.

编辑:可能很明显,但也将 */ 添加到 git ignore 以忽略子文件夹。