跳转至

Git 判断仓库是否为浅克隆

本文信息

创建时间: 2022.03.23 19:34:09

更新时间: 2022.11.05 02:01:07

git 1.15+

直接使用git命令判断

Bash
git rev-parse --is-shallow-repository

git 1.15-

考虑使用shell实现,此处使用bash环境,Linux主机

Bash
[ -f $(git rev-parse --git-dir)/shallow ] && echo true || echo false

GitPython

Python
1
2
3
4
repo = git.Repo(repo_path)
type(repo.git).GIT_PYTHON_TRACE = 'full'

is_shallow = repo.git.rev_parse(['--is-shallow-repository'])  # ‘true' or 'false

参考资料:

  1. How to Test if Git Repository is Shallow? - Stack Overflow
  2. How to convert a Git shallow clone to a full clone? - Stack Overflow