如何使浅的 git clone 保持最新(同时保持浅)

ide*_*n42 4 git

在某些情况下,存储整个 git 历史记录(例如构建机器人)没有用。

是否可以对 git 存储库(master例如,具有单个分支)进行浅层克隆,并使其保持最新状态,同时保持浅层?

ide*_*n42 5

是的,这是可能的,以下 git 命令是 shell 脚本函数,但实际上它们可能是 bat 文件或类似文件。

# clone
function git_shallow_clone() {
    git clone --depth 1 --single-branch $@
}
Run Code Online (Sandbox Code Playgroud)
# pull
function git_shallow_pull() {
    git pull --no-tags $@

    # clean-up, if a new revision is found
    git show-ref -s HEAD > .git/shallow
    git reflog expire --expire=0
    git prune
    git prune-packed
}
Run Code Online (Sandbox Code Playgroud)
# make an existing clone shallow (handy in some cases)
function git_shallow_make() {

    # delete all branches except for the current branch
    git branch -D `git branch | grep -v $(git rev-parse --abbrev-ref HEAD)`
    # delete all tags
    git tag -d `git tag | grep -E '.'`
    # delete all stash
    git stash clear


    # clean-up, if a new revision is found (same as above)
    git show-ref -s HEAD > .git/shallow
    git reflog expire --expire=0
    git prune
    git prune-packed
}
Run Code Online (Sandbox Code Playgroud)
# load history into a shallow clone.
function git_shallow_unmake() {
    git fetch --no-tags --unshallow
}
Run Code Online (Sandbox Code Playgroud)

笔记

  • --no-tags使用很重要,否则您可能会克隆具有sha1指向分支外 blob 的 s 的标签master
  • 限制到单个分支也很有用,假设您只对master或至少一个分支感兴趣
  • 每次拉动后重新执行浅层似乎是一项繁重的操作,但我没有找到更好的方法

感谢:https : //stackoverflow.com/a/7937916/432509(对于这个答案的重要部分)