我有文件夹应用程序/,我添加到.gitignore。在应用程序/文件夹中是文件夹application/language/gr。我如何包含这个文件夹?

我试过了

application/
!application/language/gr/

当前回答

添加一个名为.gitignore的文件到子文件夹,然后填充

!/Bin/

这对我很管用!

其他回答

如果您排除了application/,那么它下面的所有内容将始终被排除(即使稍后的某些负排除模式(“unignore”)可能匹配application/下的某些内容)。

要做你想做的事情,你必须“取消忽略”你想“取消忽略”的所有父目录。通常情况下,您会成对编写针对这种情况的规则:忽略目录中的所有内容,但不包括某些子目录。

# you can skip this first one if it is not already excluded by prior patterns
!application/

application/*
!application/language/

application/language/*
!application/language/gr/

请注意 后面的/*是重要的:

模式dir/排除了名为dir的目录和(隐式地)该目录下的所有内容。 使用dir/, Git永远不会查看dir下的任何内容,因此永远不会对dir下的任何内容应用任何“不排除”模式。 模式dir/*没有说明dir本身;它只是排除了dir下的所有东西。 使用dir/*, Git将直接处理dir的内容,让其他模式有机会“取消排除”一些内容(!dir/sub/)。

关于这个问题还有很多类似的问题,所以我会把我之前写过的贴出来:

我让这个在我的机器上工作的唯一方法是这样做:

# Ignore all directories, and all sub-directories, and it's contents:
*/*

#Now ignore all files in the current directory 
#(This fails to ignore files without a ".", for example 
#'file.txt' works, but 
#'file' doesn't):
*.*

#Only Include these specific directories and subdirectories:
!wordpress/
!wordpress/*/
!wordpress/*/wp-content/
!wordpress/*/wp-content/themes/
!wordpress/*/wp-content/themes/*
!wordpress/*/wp-content/themes/*/*
!wordpress/*/wp-content/themes/*/*/*
!wordpress/*/wp-content/themes/*/*/*/*
!wordpress/*/wp-content/themes/*/*/*/*/*

请注意,您必须显式地允许您想要包含的每个级别的内容。所以如果我在主题下面有子目录5,我仍然需要拼写出来。

这是@Yarin的评论:https://stackoverflow.com/a/5250314/1696153

这些都是有用的话题:

负模式在。gitignore中是如何工作的? gitignore排除规则实际上是如何工作的?

我也试过

*
*/*
**/**

和* * / wp-content /主题/ * *

或/ wp-content /主题/ * * / *

这些对我也没用。大量的试验和错误!

我想跟踪位于/etc/nagios/中的Nagios配置文件以及/usr/lib64/ Nagios /plugins/中的插件。为此,我在/中初始化了一个git repo,并使用了以下排除列表:

/*
!etc
etc/*
!etc/nagios
!usr
usr/*
!usr/lib64
usr/lib64/*
!usr/lib64/nagios
usr/lib64/nagios/*
!usr/lib64/nagios/plugins

Git像这样遍历列表:

/*             exclude everything under / ...
!etc           but include /etc back
etc/*             exclude everything under /etc/...
!etc/nagios       but include /etc/nagios back
!usr           but include /usr back
usr/*             exclude everything under /usr/...
                  and so on...

我的JetBrains IntelliJ IDEA .gitignore配置,在那里我需要排除整个。IDEA文件夹除了。IDEA /runConfigurations:

.idea
!.idea/
.idea/*
!.idea/runConfigurations/

参见:https://github.com/daggerok/gitignore-idea-runConfigurations

最简单也可能是最好的方法是手动添加文件(通常这优先于.gitignore风格的规则):

git add /path/to/module

如果文件已经被忽略,则可能需要-f。您甚至可能希望使用-N意图添加标志,以建议您将添加它们,但不是立即添加。我经常对我还没有准备好的新文件这样做。


这是一个答案的副本张贴在什么很容易是一个重复的QA。我在这里重新发布是为了增加能见度——我发现不搞乱gitignore规则更容易。