git Git忽略目录中除子文件夹之外的所有内容

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

Git Ignore everything in a directory except subfolders

gitgitignoresubdirectory

提问by nickel715

This is my folder structure:

这是我的文件夹结构:

data/
    .gitignore
    uploads/
        .gitignore

I would like to commit the folders but not the files inside them.

我想提交文件夹而不是其中的文件。

So I add a .gitignore files in every folder with the following content:

所以我在每个文件夹中添加了一个 .gitignore 文件,内容如下:

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

The problem is that *matches also on directories so git tracks only data/.gitignore

问题是也*匹配目录所以 git 只跟踪data/.gitignore

采纳答案by nickel715

The solution is quite easy, add !*/to the .gitignore files and only files in the current folder will be ignored

解决方法很简单,添加!*/到 .gitignore 文件中,只会忽略当前文件夹中的文件

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

回答by kaiser

Please don't misuse.gitignorefiles. Better stick to default ways to go on this, so later developers can quickly get into your project.

请不要滥用.gitignore文件。最好坚持使用默认方式进行此操作,以便以后的开发人员可以快速进入您的项目。

  1. Add an empty .gitkeepfile in the folders that you want to commit without the files
  2. Exclude the folders, but not the .gitkeepfrom your main.gitignorefile.

    folder/*
    !folder/.gitkeep
    
  1. .gitkeep在要提交的文件夹中添加一个空文件而不包含这些文件
  2. 排除的文件夹,而不是.gitkeep从你的.gitignore文件。

    folder/*
    !folder/.gitkeep
    

This ignores all files in a folder, but not the .gitkeepfile. Now the folder will be commited with only the .gitkeepfile as content.

这会忽略文件夹中的所有文件,但不会忽略该.gitkeep文件。现在该文件夹将仅以.gitkeep文件作为内容提交。

回答by e18r

Try this:

尝试这个:

*.*
!.gitignore
!*/*