[英]How do I tell Git to ignore everything except a subdirectory?
I want to ignore all files in my repository except those that occur in the bin
subdirectory.我想忽略存储库中的所有文件,除了bin
子目录中出现的文件。 I tried adding the following to my .gitignore
:我尝试将以下内容添加到我的.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?有什么建议?
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 目录,包括子目录和它们的文件。
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
You must exclude everything on the way to the destinations, but you must include the destinations:您必须排除前往目的地途中的所有内容,但必须包括目的地:
Note: This is an exclusion file (ie .gitignore) so the logic is inverted.注意:这是一个排除文件(即 .gitignore),所以逻辑颠倒了。 Ignore all, except the tsp directory, ignore all in tsp directory, except the src directory...忽略所有,除了 tsp 目录,忽略 tsp 目录中的所有,除了 src 目录...
/*
!/tsp
/tsp/*
!/tsp/src
/tsp/src/*
!/tsp/src/*.h
!/tsp/src/*.cpp
!/tsp/src/data.txt
!/.gitignore
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
directories that are in subdirectories that aren't called bin
.我更喜欢第二个解决方案,因为第一个解决方案不会停止忽略bin
目录中未称为bin
子目录中的文件。 This may or may not matter in your situation.这在您的情况下可能重要也可能无关紧要。
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 中使用前导斜线。
Try following in latest GIT version.尝试使用最新的 GIT 版本。
*
!*/
!/path/to/your/dir/**
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.它不会忽略所有文件,它只会忽略顶级文件,而不是您不想忽略的目录中的文件。
This .gitignore
works for me:这个.gitignore
对我.gitignore
:
*/
/*
!bin/
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:先试一下再评论。
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 有用。
Here is my solution.这是我的解决方案。 In my scenario I had a folder with subfolders as follow:在我的场景中,我有一个包含子文件夹的文件夹,如下所示:
And from the database folder I wanted to push the sql folder only so my .gitignore
file was as follow:从数据库文件夹中,我只想推送sql文件夹,因此我的.gitignore
文件如下:
database/*
!database/sql/
Where the first line simply say ignore all subfolder(s) of the database folder and the second line meaning since you are ignoring all subfolder(s) of the database folder exclude(don't ignore) the sql subfolder第一行简单地说忽略数据库文件夹的所有子文件夹,第二行意思是因为你忽略了数据库文件夹的所有子文件夹排除(不要忽略) sql子文件夹
Late to the party, but none of the answers worked for me "out of the box".聚会迟到了,但没有一个答案对我“开箱即用”有效。 This one does:这个是:
*
!bin
!bin/**
To me it turns out I should not ignore the parent folder but files and folders in it using '/*'对我来说,事实证明我不应该忽略父文件夹,而是使用“/*”忽略其中的文件和文件夹
-- **/node_modules -- **/node_modules
++ * /node_modules/ ++ * /节点模块/
then然后
++ !node_modules/@my-package/ ++ !node_modules/@my-package/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.