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

我试过了

application/
!application/language/gr/

当前回答

对于Git 1.9/2.0(2014年第一季度),Karsten Blees (kblees)的提交59856de澄清了这种情况:

Gitignore.txt:阐明排除目录的递归性质

可选前缀“!”,否定模式;任何被前一个模式排除的匹配文件将被重新包含。 如果排除了文件的父目录,则无法重新包含该文件。(*) (*:除非git 2.8+满足某些条件,见下文) 出于性能原因,Git没有列出排除的目录,因此所包含文件上的任何模式都没有影响,无论它们定义在哪里。 对于以文字“!”开头的模式,在第一个“!”前面加一个反斜杠(“\”),例如,“\!important!.txt”。 示例排除除特定目录foo/bar以外的所有内容(注意/* -没有斜杠,通配符也会排除foo/bar内的所有内容):

 --------------------------------------------------------------
     $ cat .gitignore
     # exclude everything except directory foo/bar
     /*
     !/foo
     /foo/*
     !/foo/bar
 --------------------------------------------------------------

在你的情况下:

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

必须先将文件夹列入白名单,然后才能将给定文件夹中的文件列入白名单。


2016年2月/ 3月更新:

注意,在git 2.9.x/2.10(2016年年中?)中,如果重新包含的路径中没有通配符,则该文件的父目录被排除在外,则可能重新包含该文件。

nguyforgen Thái ngeconc Duy (pclouds)正在尝试添加这个特性:

git v2.7.0提交506d8f1, git v2.8.0-rc0提交76b620d Commit 5e57f9c git v2.8.0-rc0在提交5cee3493 git 2.8.0时被恢复(!)。

因此,在git 2.9+中,这实际上是可以工作的,但最终被恢复了:

application/
!application/language/gr/

其他回答

如果您排除了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/)。

我经常在CLI中使用这个解决方案,而不是配置我的.gitignore,我创建了一个单独的.include文件,在其中定义我想包含的(子)目录,尽管目录直接或递归被.gitignore忽略。

因此,我额外使用

git add `cat .include`

在登台期间,提交之前。

对于OP,我建议使用.include,其中包含这些行:

<parent_folder_path>/application/language/gr/*

注意:使用cat不允许使用别名(在.include内)来指定$HOME(或任何其他特定目录)。这是因为行homedir/app1/* 当使用上述命令传递给git add时,显示为git add 'homedir/app1/*',并且将字符括在单引号(")中保留了引号内每个字符的文字值,从而防止别名(如homedir)发挥作用(参见Bash单引号)。

下面是我在这里的repo中使用的.include文件的示例。

/home/abhirup/token.txt
/home/abhirup/.include
/home/abhirup/.vim/*
/home/abhirup/.viminfo
/home/abhirup/.bashrc
/home/abhirup/.vimrc
/home/abhirup/.condarc

@Chris Johnsen的回答很好,但在Git的新版本(1.8.2或更高版本)中,有一个双星号模式,你可以利用它来获得更简洁的解决方案:

# assuming the root folder you want to ignore is 'application'
application/**/*

# the subfolder(s) you want to track:
!application/language/gr/

这样你就不必“取消忽略”你想要跟踪的子文件夹的父目录。


Git 2.17.0(不知道在这个版本之前有多早。可能回到1.8.2),使用**模式结合排除法对指向您的文件的每个子目录有效。例如:

# assuming the root folder you want to ignore is 'application'
application/**

# Explicitly track certain content nested in the 'application' folder:
!application/language/
!application/language/gr/
!application/language/gr/** # Example adding all files & folder in the 'gr' folder
!application/language/gr/SomeFile.txt # Example adding specific file in the 'gr' folder

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

!/Bin/

这对我很管用!

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

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

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