If your Git is 2.15 or later, run:
git rev-parse --is-shallow-repository
将会打印 false
(非浅拷贝)或者 true
(浅拷贝):
if $(git rev-parse --is-shallow-repository); then
... repository is shallow ...
fi
以下答案适用于Git 2.15版本之前。
如果您使用的Git版本早于2.15,1只需在Git存储库目录中测试文件shallow
: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
将显示当前的版本号:
$ git --version
2.14.1
$ git --version
git version 2.7.4
等等。(我现在有不同版本的虚拟机/机器。)您还可以运行:
git rev-parse --is-shallow-repository
如果它只打印--is-shallow-repository
,那么你的Git版本低于2.15,缺少该选项。
2要了解为什么在$(git rev-parse --git-dir)
周围有双引号,请参见Tom Hale的评论。请注意,测试这个有点棘手,因为从如/tmp/with space
的仓库的顶层运行git rev-parse --git-dir
只会打印出.git
; 您必须处于子目录中,例如/tmp/with space/sub
才能观察到该问题。