Gitignore 除了子文件夹(包含所有内容)之外的所有内容?
Gitignore everything except a subfolder (with every content)?
我想忽略除特定子文件夹(及其所有内容!)之外的所有内容。我尝试了可能重复问题的解决方案,但没有成功。
我需要一些简单的东西,例如:
*
!That/Very/Folder/*
但这不起作用
I want to ignore everything
将文件夹添加到 gitignore
except a specific subfolder (and all of its contents!).
强制将文件夹添加到存储库
git add -f folder
编辑:
例如,当我需要保留日志文件夹而不是其内容时,我会使用此解决方案。通常,当我认为永远不会添加文件夹的内容时。通常我只添加带有 -f
选项的 path/to/folder/.gitkeep
文件。
*
!*/
!That/Very/Folder/**
!Also/This/Another/Folder/**
忽略所有内容,允许子文件夹 (!),然后允许特定文件夹内容(其中的子文件夹不受限制)。
Credits to @Jepessen for the middle piece that makes it work.
你的 .gitignore
几乎可以工作,但它不是一个简单的原因:第一条规则 (*
) 告诉 Git 忽略根目录中的每个文件和目录存储库。 Git 尊重它并忽略所有内容,包括 That
目录及其内容。后面的 "unignore" 规则不匹配 That
子目录中的任何内容,因为 That
目录与其内容一起被忽略,并且它们没有效果。
为了告诉 Git 不要忽略深层嵌套子目录中的文件和目录,您必须编写忽略和取消忽略规则,让它首先到达封闭的子目录,然后添加规则你要。
您的 .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/*
我想忽略除特定子文件夹(及其所有内容!)之外的所有内容。我尝试了可能重复问题的解决方案,但没有成功。
我需要一些简单的东西,例如:
*
!That/Very/Folder/*
但这不起作用
I want to ignore everything
将文件夹添加到 gitignore
except a specific subfolder (and all of its contents!).
强制将文件夹添加到存储库
git add -f folder
编辑:
例如,当我需要保留日志文件夹而不是其内容时,我会使用此解决方案。通常,当我认为永远不会添加文件夹的内容时。通常我只添加带有 -f
选项的 path/to/folder/.gitkeep
文件。
*
!*/
!That/Very/Folder/**
!Also/This/Another/Folder/**
忽略所有内容,允许子文件夹 (!),然后允许特定文件夹内容(其中的子文件夹不受限制)。
Credits to @Jepessen for the middle piece that makes it work.
你的 .gitignore
几乎可以工作,但它不是一个简单的原因:第一条规则 (*
) 告诉 Git 忽略根目录中的每个文件和目录存储库。 Git 尊重它并忽略所有内容,包括 That
目录及其内容。后面的 "unignore" 规则不匹配 That
子目录中的任何内容,因为 That
目录与其内容一起被忽略,并且它们没有效果。
为了告诉 Git 不要忽略深层嵌套子目录中的文件和目录,您必须编写忽略和取消忽略规则,让它首先到达封闭的子目录,然后添加规则你要。
您的 .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/*