简体   繁体   English

Gitignore 除子文件夹外的所有内容(包含所有内容)?

[英]Gitignore everything except a subfolder (with every content)?

I want to ignore everything, except a specific subfolder (and all of its contents!).我想忽略所有内容,除了特定的子文件夹(及其所有内容!)。 I tried solutions from possible duplicate questions without any success.我尝试了可能的重复问题的解决方案,但没有成功。

I'd need something simple like:我需要一些简单的东西,比如:

*
!That/Very/Folder/*

But this is not working 😞但这不起作用😞

*
!*/
!That/Very/Folder/**
!Also/This/Another/Folder/**

Ignore everything, allow subfolders (!) , then allow specific folder contents (with unlimited subfolders within).忽略所有内容,允许子文件夹 (!) ,然后允许特定文件夹内容(其中包含无限子文件夹)。

Credits to @Jepessen for the middle piece that makes it work.归功于@Jepessen使其工作的中间部分。

Your .gitignore almost works but it doesn't for a simple reason: the first rule ( * ) tells Git to ignore every file and directory in the root of the repository.您的.gitignore几乎可以工作,但原因并不简单:第一条规则 ( * ) 告诉 Git 忽略存储库根目录中的每个文件和目录。 Git honors it and ignores everything, including the That directory and its content. Git的荣誉,并忽略一切,包括That目录及其内容。 The "unignore" rules that follow do not match anything inside the That subdirectory because the That directory is ignored together with it content, and they don't have effect.遵循的“unignore”规则不匹配That子目录中的任何内容,因为That目录与其内容一起被忽略,并且它们不起作用。

In order to tell Git to not ignore files and directories in a deeply nested sub-directory you have to write ignore and unignore rules to let it reach the enclosing sub-directory first and then add the rules you want.为了告诉 Git 不要忽略深层嵌套子目录中的文件和目录,您必须编写 ignore 和 unignore 规则,让它首先到达封闭的子目录,然后添加您想要的规则。

Your .gitignore file should look like this:您的.gitignore文件应如下所示:

### Ignore everything ###
*

# But do not ignore "That" because we need something from its internals...
!That/

# ... but ignore (almost all) the content of "That"...
That/*
# ... however, do not ignore "That/Very" because we need to dig more into it
!That/Very/

# ... but we don't care about most of the content of "That/Very"
That/Very/*
# ... except for "That/Very/Folder" we care
!That/Very/Folder/
# ... and its content
!That/Very/Folder/*

I want to ignore everything我想忽略一切

add the folder to the gitignore将文件夹添加到 gitignore

except a specific subfolder (and all of its contents!).除了特定的子文件夹(及其所有内容!)。

force folder to be added to the repository强制将文件夹添加到存储库

git add -f folder

EDIT:编辑:

I use this solution for example when I need to keep log folder, for example, but not its content.例如,当我需要保留日志文件夹而不是其内容时,我会使用此解决方案。 Generally, when I suppose the content of the folder is never to be added.通常,当我认为永远不会添加文件夹的内容时。 And generally I add just path/to/folder/.gitkeep file with -f option.通常我只添加带有-f选项的path/to/folder/.gitkeep文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM