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

我试过了

application/
!application/language/gr/

当前回答

@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

其他回答

因为很多程序员使用node。满足这个问题的用例是排除node_modules,除了一个模块module-a,例如:

!node_modules/

node_modules/*
!node_modules/module-a/

我发现只有这个可行。

**/node_modules/*
!**/node_modules/keep-dir

我想跟踪jquery生产js文件,这是有效的:

node_modules/*
!node_modules/jquery
node_modules/jquery/*
!node_modules/jquery/dist/*

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

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

# 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 /主题/ * * / *

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

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

!/Bin/

这对我很管用!