如果您的 Git 是 2.15 或更高版本,请运行:
git rev-parse --is-shallow-repository
将打印false(不浅)或true(浅):
if $(git rev-parse --is-shallow-repository); then
... repository is shallow ...
fi
下面的答案可以追溯到 2.15 之前的 Git 版本。
如果您的 Git 版本早于 2.15,1 只需在 Git 存储库目录中测试文件 shallow:
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 将打印当前版本号:
$ git --version
2.14.1
$ git --version
git version 2.7.4
等等。 (此时我在不同的虚拟机/机器上有多个版本。)你也可以运行:
git rev-parse --is-shallow-repository
如果它只打印 --is-shallow-repository,那么您的 Git 是 2.15 之前的版本并且缺少该选项。