我有一个 foo
包含 2 个文件的目录,bar
和 baz
.我有以下 .gitignore
文件,正上方 foo
目录:
foo/
!foo/baz
我希望这会忽略
foo
中的所有内容目录除了它应该不是 忽略 baz
.然而,一切都在
foo
仍然被忽视。如果我运行 git check-ignore foo/*
,我得到:foo/bar
foo/baz
我究竟做错了什么?
最佳答案
.gitignore 不支持该特定公式:
An optional prefix "
!
" which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined. Put a backslash ("\
") in front of the first "!
" for patterns that begin with a literal "!
", for example, "\!important!.txt
".
相反,你可以这样做:
foo/*
!foo/baz
这是一个示例 session ,以便您可以看到它的运行情况:
:: tree
.
`-- foo/
|-- bar
`-- baz
:: cat .gitignore
foo/*
!foo/baz
:: git status -sb
## Initial commit on master
?? .gitignore
?? foo/
:: git add foo
:: git status -sb
## Initial commit on master
A foo/baz
?? .gitignore
请注意,添加了 baz 但忽略了 bar。
要查看如果 baz 不在 foo 中会是什么样子,我们可以重置然后删除 baz:
:: git reset
:: rm foo/baz
:: git status -sb
## Initial commit on master
?? .gitignore
请注意,即使 foo/bar 仍然存在, foo 也不会出现在这里。
关于git - bang 似乎不排除 .gitignore 中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41559572/