仅当文件夹不在其他文件夹中时才忽略该文件夹

时间:2012-05-03 09:54:44

标签: git

我只会忽略bin文件夹(在.gitignore中),如果它不是packages文件夹的一部分。是否可能/?

更新

我想排除以下情况:

packages/bin
packages/somefolder/bin
etc.

1 个答案:

答案 0 :(得分:3)

您可以在.gitignore

中创建否定的条目
bin/
!packages/bin/
!packages/*/bin/
!packages/*/*/bin/

不幸的是,git不支持多个目录级别的** glob(或regexp匹配),因此要忽略packages/whatever/binpackages/whatever/whatever/bin,您需要两个条目(依此类推,以便进行更深层的嵌套) )。

演示:

[adrian@hades:/tmp/gitignore:master]> git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       foo/bar/bin/
#       foo/baz/bin/
#       packages/bin/
#       packages/xxx/bin/
#       packages/xxx/yyy/bin/
#       xxx/packages/bin/
nothing added to commit but untracked files present (use "git add" to track)

[adrian@hades:/tmp/gitignore:master]> cat > .gitignore
bin/
!packages/bin/
!packages/*/bin/
!packages/*/*/bin/

[adrian@hades:/tmp/gitignore:master]> git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       .gitignore
#       packages/bin/
#       packages/xxx/bin/
#       packages/xxx/yyy/bin/
nothing added to commit but untracked files present (use "git add" to track)