如何使一个浅git克隆保持最新(同时保持浅)

时间:2015-01-08 16:08:35

标签: git

在某些情况下,它无法存储整个git历史记录(例如build-bot)。

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

1 个答案:

答案 0 :(得分:4)

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

# clone
function git_shallow_clone() {
    git clone --depth 1 --single-branch $@
}

# pull
function git_shallow_pull() {
    g 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
}

# 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
}

# load history into a shallow clone.
function git_shallow_unmake() {
    git fetch --no-tags --unshallow
}

备注

  • --no-tags非常重要,否则您可以克隆sha1指向blob位于分支master之外的标记。
  • 限制到单个分支也很有用,假设您只对master感兴趣,或者至少对一个分支感兴趣。
  • 每次拉动后重新执行浅显似乎是一个很重的操作,但我找不到更好的方法。

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

相关问题