繁体   English   中英

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

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

我想忽略所有内容,除了特定的子文件夹(及其所有内容!)。 我尝试了可能的重复问题的解决方案,但没有成功。

我需要一些简单的东西,比如:

*
!That/Very/Folder/*

但这不起作用😞

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

忽略所有内容,允许子文件夹 (!) ,然后允许特定文件夹内容(其中包含无限子文件夹)。

归功于@Jepessen使其工作的中间部分。

您的.gitignore几乎可以工作,但原因并不简单:第一条规则 ( * ) 告诉 Git 忽略存储库根目录中的每个文件和目录。 Git的荣誉,并忽略一切,包括That目录及其内容。 遵循的“unignore”规则不匹配That子目录中的任何内容,因为That目录与其内容一起被忽略,并且它们不起作用。

为了告诉 Git 不要忽略深层嵌套子目录中的文件和目录,您必须编写 ignore 和 unignore 规则,让它首先到达封闭的子目录,然后添加您想要的规则。

您的.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/*

我想忽略一切

将文件夹添加到 gitignore

除了特定的子文件夹(及其所有内容!)。

强制将文件夹添加到存储库

git add -f folder

编辑:

例如,当我需要保留日志文件夹而不是其内容时,我会使用此解决方案。 通常,当我认为永远不会添加文件夹的内容时。 通常我只添加带有-f选项的path/to/folder/.gitkeep文件。

暂无
暂无

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

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