如何告诉 Git 忽略除子目录之外的所有内容?
我想忽略存储库中的所有文件,除了 bin
子目录中出现的文件。 我尝试将以下内容添加到我的 .gitignore
中:
*
!bin/*
但是,这没有达到预期的效果:我在 bin/
内部创建了一个新文件,但执行 git状态
仍然显示没有任何可提交的内容(工作目录干净)
。
有什么建议么?
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/*
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)
.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
这会忽略根文件和 root 目录,然后取消忽略 root bin 目录:
这样您就可以获得所有 bin 目录,包括子目录及其文件。
This ignores root files & root directories, then un-ignores the root bin directory:
This way you get all of the bin directory, including subdirectories and their files.
您遇到的唯一问题是
bin
目录本身与bin/*
模式不匹配,因此 git 甚至不会在bin
中查找> 目录。我想到了两种解决方案。
.gitignore
:或
.gitignore
:bin/.gitignore
:我更喜欢第二个解决方案,因为第一个解决方案不会停止忽略
bin
目录位于不名为bin
的子目录中。 这对于您的情况可能很重要,也可能无关紧要。The only issue you have is that the
bin
directory itself is not matched by thebin/*
pattern so git isn't even look in thebin
directory.There are two solutions that spring to mind.
.gitignore
:or
.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 calledbin
. This may or may not matter in your situation.您必须排除前往目的地途中的所有内容,但必须包括目的地:
注意:这是一个排除文件(即 .gitignore),因此逻辑是相反的。 忽略除 tsp 目录之外的所有内容,忽略 tsp 目录中除 src 目录之外的所有内容...
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...
这里如何忽略某个目录
结构中除一个目录“MY_SUPER_DUPER_TEMPLATE_directory”之外的所有内容:/bitrix/templates/MY_SUPER_DUPER_TEMPLATE_directory
Here how to ignore everything exept one directory "MY_SUPER_DUPER_TEMPLATE_directory" in some directory
Structure: /bitrix/templates/MY_SUPER_DUPER_TEMPLATE_directory
从官方 git doc 中,其中一个示例表示:
Example to排除除特定目录 foo 之外的所有内容/bar (注意 /* - 没有斜杠,通配符也会排除 foo/bar 中的所有内容):
有关前导斜杠的说明: 何时在 gitignore 中使用前导斜杠。
From the official git doc, one of the examples says:
Example to exclude everything except a specific directory foo/bar (note the /* - without the slash, the wildcard would also exclude everything within foo/bar):
For an explanation about the leading slash: When to use leading slash in gitignore.
在最新的 GIT 版本中尝试以下操作。
Try following in latest GIT version.
这是我的解决方案。 在我的场景中,我有一个包含子文件夹的文件夹,如下所示:
并且从database文件夹中我只想推送sql文件夹,所以我的
.gitignore
文件如下:第一行只是说忽略 database 文件夹的所有子文件夹,第二行的意思是因为您忽略 database 文件夹的所有子文件夹>database 文件夹排除(不要忽略)sql 子文件夹
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: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
对我来说,事实证明我不应该忽略父文件夹,而是使用
/*
忽略其中的文件和文件夹
To me it turns out I should not ignore the parent folder but files and folders in it using
/*
then
我正在开发一个 WordPress 插件,这是我的
.gitignore
仅跟踪插件文件I'm developing a wordpress plugin, Here is my
.gitignore
to only track the plugin files这个 .gitignore 对我有用:
This
.gitignore
works for me:试试这个不起眼的 git bug。
希望它有帮助:)
支持文档 git bug 示例
PS:在发表评论之前先尝试一下。
try this obscure git bug.
Hope it helps :)
Supporting docs git bug sample
PS: try it first before commenting.
我最近不得不这样做:
我不确定为什么我需要两行来排除一个文件夹,但这对我和 macos 来说都是有效的。
I had to do this recently:
I'm not sure why I need two lines for to exclude one folder, but this is what worked for me and macos.
我认为更好的方法是通过以斜杠开头的模式将每个模式锚定到顶部 git 目录:
它不会忽略所有文件,它只会忽略顶级文件,而不是您不想忽略的目录中的文件。
I think a better way would be to anchor each pattern to the top git directory by starting the pattern with a slash:
Instead of ignoring all files it will only ignore top level files, not the ones in the directory you dont want to ignore.
聚会迟到了,但没有一个答案对我来说“开箱即用”。 这个的作用是:
Late to the party, but none of the answers worked for me "out of the box". This one does: