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

我试过了

application/
!application/language/gr/

当前回答

我在这里发现了一个类似的情况,在laravel中默认情况下,.gitignore忽略所有使用asterix的内容,然后覆盖公共目录。 (这也是与主答案@Chris Johnsen相同的解决方案,只是可能更简洁一点。)

*
!public
!.gitignore

如果您遇到OP场景,这是不够的。

如果你想提交一个特定的public子文件夹,例如,在你的public/products目录中,你想包含一个子文件夹深度的文件,例如,包含public/products/a/b.jpg,它们不会被正确检测到,即使你像这样特别地添加它们!/public/products, !public/products/*等。

解决方案是确保像这样为每个路径级别添加一个条目来覆盖它们。

*
!.gitignore
!public/
!public/*/
!public/products/
!public/products/*
!public/products/*/
!public/products/*/
!public/products/*/*

其他回答

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

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

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

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

在WordPress中,这帮助了我:

wp-admin/
wp-includes/
/wp-content/*
!wp-content/plugins/
/wp-content/plugins/*
!/wp-content/plugins/plugin-name/
!/wp-content/plugins/plugin-name/*.*
!/wp-content/plugins/plugin-name/**

类似于这条评论,没有一个解决方案和模式适合我;强制git添加应该被排除的文件和文件夹,有效:

Git添加-f。

@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