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

我试过了

application/
!application/language/gr/

当前回答

特别是对于较旧的Git版本,大多数建议都不能很好地工作。 如果是这种情况,我将在我希望包含内容的目录中放置一个单独的.gitignore,而不考虑其他设置,并允许其中包含所需的内容。

例如: / .gitignore

# ignore all .dll files
*.dll

/ dependency_files .gitignore

# include everything
!*

因此,/dependency_files中的所有内容(甚至是.dll文件)都被很好地包含在内。

其他回答

特别是对于较旧的Git版本,大多数建议都不能很好地工作。 如果是这种情况,我将在我希望包含内容的目录中放置一个单独的.gitignore,而不考虑其他设置,并允许其中包含所需的内容。

例如: / .gitignore

# ignore all .dll files
*.dll

/ dependency_files .gitignore

# include everything
!*

因此,/dependency_files中的所有内容(甚至是.dll文件)都被很好地包含在内。

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

!node_modules/

node_modules/*
!node_modules/module-a/

@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 -指定有意忽略的未跟踪文件。

示例排除除特定目录foo/bar以外的所有内容(注意/* -没有斜杠,通配符也会排除foo/bar内的所有内容):

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

WordPress的另一个例子:

!/wp-content
wp-content/*
!/wp-content/plugins
wp-content/plugins/*
!wp-content/plugins/my-awesome-plugin

更多信息在这里:https://git-scm.com/docs/gitignore

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

!/Bin/

这对我很管用!