如果你的 Git 是2.15 https://github.com/git/git/blob/v2.15.0/Documentation/RelNotes/2.15.0.txt#L87-L89或稍后,运行:
git rev-parse --is-shallow-repository
这将打印false
(不浅)或true
(浅的):
if $(git rev-parse --is-shallow-repository); then
... repository is shallow ...
fi
下面的答案可以追溯到 2.15 之前的 Git 版本。
If your Git is older than 2.15,1 just test for the file shallow
in the Git repository directory:2
if [ -f "$(git rev-parse --git-dir)"/shallow ]; then
echo this is a shallow repository;
else
echo not a shallow repository;
fi
或(较短):
[ -f "$(git rev-parse --git-dir)"/shallow ] && echo true || echo false
您可以将其转换为 shell 函数:
test_shallow() {
[ -f "$(git rev-parse --git-dir)"/shallow ] && echo true || echo false
}
甚至自动执行 Git 版本检查:
test_shallow() {
set -- $(git rev-parse --is-shallow-repository)
if [ x$1 == x--is-shallow-repository ]; then
[ -f "$(git rev-parse --git-dir)"/shallow ] && set true || set false
fi
echo $1
}
1git --version
will print the current version number:
$ git --version
2.14.1
$ git --version
git version 2.7.4
等等(此时我在不同的虚拟机/机器上有多个版本。)您还可以运行:
git rev-parse --is-shallow-repository
如果只是打印--is-shallow-repository
,您的 Git 是 2.15 之前的版本并且缺少该选项。
2To see why there are double quotes around $(git rev-parse --git-dir)
, see Tom Hale's comment https://stackoverflow.com/questions/37531605/how-to-test-if-git-repository-is-shallow/37533086?noredirect=1#comment125560270_37533086. Note that testing this is a bit tricky since git rev-parse --git-dir
from the top level of, e.g., the repository /tmp/with space
just prints .git
; you must be in a subdirectory, such as /tmp/with space/sub
to observe the problem.