我如何告诉 Git 忽略除子目录之外的所有内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1248570/
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
How do I tell Git to ignore everything except a subdirectory?
提问by Michael Goerz
I want to ignore all files in my repository except those that occur in the bin
subdirectory. I tried adding the following to my .gitignore
:
我想忽略我的存储库中的所有文件,除了那些出现在bin
子目录中的文件。我尝试将以下内容添加到我的.gitignore
:
*
!bin/*
This does not have the desired effect, however: I created a new file inside of bin/
, but doing git status
still shows nothing to commit (working directory clean)
.
然而,这并没有达到预期的效果:我在 中创建了一个新文件bin/
,但git status
仍然显示nothing to commit (working directory clean)
.
Any suggestions?
有什么建议?
回答by Tyler Laing
This ignores root files & root directories, then un-ignores the root bin directory:
这将忽略根文件和根目录,然后取消忽略根 bin 目录:
/*
/*/
!/bin/
This way you get all of the bin directory, including subdirectories and their files.
这样你就可以得到所有的 bin 目录,包括子目录和它们的文件。
回答by Andrii Tishchenko
Here how to ignore everything exept one directory "MY_SUPER_DUPER_TEMPLATE_directory" in some directory
这里如何忽略某些目录中除一个目录“ MY_SUPER_DUPER_TEMPLATE_directory”之外的所有内容
Structure: /bitrix/templates/MY_SUPER_DUPER_TEMPLATE_directory
结构:/bitrix/templates/ MY_SUPER_DUPER_TEMPLATE_directory
/*
!/bitrix
/bitrix/*
!/bitrix/templates
/bitrix/templates/*
!/bitrix/templates/MY_SUPER_DUPER_TEMPLATE_directory
*.DS_Store
*.gitignore
回答by nikc
You must exclude everything on the way to the destinations, but you must include the destinations:
您必须排除前往目的地途中的所有内容,但必须包括目的地:
Note: This is an exclusion file (i.e. .gitignore) so the logic is inverted. Ignore all, except the tsp directory, ignore all in tsp directory, except the src directory...
注意:这是一个排除文件(即 .gitignore),所以逻辑颠倒了。忽略所有,除了 tsp 目录,忽略 tsp 目录中的所有,除了 src 目录...
/*
!/tsp
/tsp/*
!/tsp/src
/tsp/src/*
!/tsp/src/*.h
!/tsp/src/*.cpp
!/tsp/src/data.txt
!/.gitignore
回答by CB Bailey
The only issue you have is that the bin
directory itself is not matched by the bin/*
pattern so git isn't even look in the bin
directory.
您遇到的唯一问题是bin
目录本身与bin/*
模式不匹配,因此 git 甚至不在bin
目录中查找。
There are two solutions that spring to mind.
脑海中浮现出两种解决方案。
.gitignore
:
.gitignore
:
*
!/bin/
!bin/*
or
或者
.gitignore
:
.gitignore
:
*
!/bin/
bin/.gitignore
:
bin/.gitignore
:
!*
I prefer the second solution as the first solution won't stop ignoring files in the bin
directory that are in subdirectories that aren't called bin
. This may or may not matter in your situation.
我更喜欢第二个解决方案,因为第一个解决方案不会停止忽略bin
目录中未调用的子目录中的文件bin
。这在您的情况下可能重要也可能无关紧要。
回答by solstice333
From the official git doc, one of the examples says:
从官方git doc,其中一个例子说:
Example to exclude everything except a specific directory foo/bar (note the /* - without the slash, the wildcard would also exclude everything within foo/bar):
排除除特定目录 foo/bar 之外的所有内容的示例(注意 /* - 没有斜杠,通配符也将排除 foo/bar 中的所有内容):
$ cat .gitignore
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar
For an explanation about the leading slash: When to use leading slash in gitignore.
有关前导斜线的解释:何时在 gitignore 中使用前导斜线。
回答by Kirby
Try following in latest GIT version.
尝试使用最新的 GIT 版本。
*
!*/
!/path/to/your/dir/**
回答by Jake
I think a better way would be to anchor each pattern to the top git directory by starting the pattern with a slash:
我认为更好的方法是通过以斜线开头的模式将每个模式锚定到顶级 git 目录:
/*
!/public_html
!/.gitignore
Instead of ignoring all files it will only ignore top level files, not the ones in the directory you dont want to ignore.
它不会忽略所有文件,它只会忽略顶级文件,而不是您不想忽略的目录中的文件。
回答by Trianam
This .gitignore
works for me:
这.gitignore
对我有用:
*/
/*
!bin/
回答by david
try this obscure git bug.
试试这个不起眼的 git bug。
!bin**/**
Hope it helps :)
希望能帮助到你 :)
Supporting docs git bug sample
支持文档git bug 示例
PS: try it first before commenting.
PS:先试一下再评论。
回答by Henry
I had to do this recently:
我最近不得不这样做:
*
!sub-dir/
!sub-dir/*
I'm not sure why I need two lines for to exclude one folder, but this is what worked for me and macos.
我不确定为什么我需要两行来排除一个文件夹,但这对我和 macos 有用。