【发布时间】:2015-02-04 18:06:41
【问题描述】:
一个 git 存储库包含两个目录:A 和 B1。 这是目录结构:
$ find -name "*.*" -print
./.git/hooks/applypatch-msg.sample
./.git/hooks/commit-msg.sample
./.git/hooks/post-update.sample
./.git/hooks/pre-applypatch.sample
./.git/hooks/pre-commit.sample
./.git/hooks/pre-push.sample
./.git/hooks/pre-rebase.sample
./.git/hooks/prepare-commit-msg.sample
./.git/hooks/update.sample
./A/a.txt
./A/A1/a1.txt
./B/b.txt
./B/B1/b1.txt
./C/c.txt
这是 .gitignore 文件,它将目录 A 和 B1 列入白名单:
$ more .gitignore
# exclude files and directories in top directory
/*
# include the A directory
!/A
# include the B directory
!/B
# exclude files and directories in B
/B/*
# include the B/B1 directory
!/B/B1
gitignore 文件基于http://git-scm.com/docs/gitignore 中的最后一个示例
从白名单目录中,如何将所有白名单目录添加到索引中? 这不起作用:
$ git add / --dry-run
fatal: /: '/' is outside repository
这适用于顶级目录:
$ git add . --dry-run
add 'A/A1/a1.txt'
add 'A/a.txt'
add 'B/B1/b1.txt'
但在白名单目录中,只有该目录被添加到索引中(B1 缺失):
$ cd A
A$ git add . --dry-run
add 'A/A1/a1.txt'
add 'A/a.txt'
类似的结果:
A$ git add * --dry-run
add 'A/A1/a1.txt'
add 'A/a.txt'
我想要一个命令来从任何列入白名单的目录中添加所有列入白名单的目录,这样我就不会忘记一个。
【问题讨论】:
-
您的“从白名单目录中”的情况完全符合预期 - 您说
git add .,这意味着“添加当前目录及其子目录”。并不是说“遍历到项目的根目录,添加整个项目”。您可以尝试使用cd A; git add ..,根据A 在层次结构中的深度酌情替换为../..、../../..等。或者git add $(git rev-parse --show-toplevel).
标签: git whitelist subdirectory