git - bang 似乎不排除 .gitignore 中的文件

标签 git

我有一个 foo包含 2 个文件的目录,barbaz .我有以下 .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/

相关文章:

git - 你可以为 git 提交启用 2FA 吗?

git - 基于每个目录的条件 gitconfig 不起作用

git - 如何为所有子域设置 Git 配置选项?

git - 如何将 Git 存储库重置为其新克隆的状态?

Git 在 Mac OS 的 screen 上自动完成

具有/多个工作站和中央服务器的 GIT 工作流

git - 是否应该将 "merge commit" merge 回 dev 分支?

mysql - 自动数据库配置脚本的空白问题

ruby-on-rails - git 克隆后 "The program ' rails ' is currently not installed"

java - git pull - 来自 Java 的更改