Git 忽略特定子目录除外

Git ignore except specific subdirectories

我看过一些关于如何忽略除某些子目录之外的所有内容的示例。

# Ignore Everything
foo/*
# Include sub directory
!foo/ccc/*

我也试过:

# Ignore Everything
foo/*
# Include sub directory
!foo/ccc/

更新

所以上面的代码有效,但是试图排除另一个目录中的另一个目录是行不通的。它仅适用于父目录和子目录。

# Ignore Everything
foo/*
# Include sub directory
!foo/ccc/aaa/ddd/ 

在我将其包含在我的 gitingore 中之后,我 运行 一个 git add --all 并且我没有在 git status.

中看到任何文件

我们将不胜感激。

您必须取消忽略路径中您希望取消忽略的每个目录。类似下面的内容应该适用于您的情况:

# Ignore everything in foo
foo/*
# Except the ccc subdir
!foo/ccc

# Ignore everything in ccc subdir
foo/ccc/*
# Except the aaa subsubdir
!foo/ccc/aaa

# Ignore everything in aaa subsubdir
foo/ccc/aaa/*
# Except the ddd subsubsubdir
!foo/ccc/aaa/ddd

忽略规则以 /* 结尾很重要,因为它告诉 git 忽略文件夹中的所有内容,但不忽略文件夹本身,允许我们为忽略规则添加例外特定子目录。