簡體   English   中英

如何使淺的 git clone 保持最新(同時保持淺)

[英]How to keep a shallow git clone up to date (while remaining shallow)

在某些情況下,存儲整個 git 歷史記錄(例如構建機器人)沒有用。

是否可以對 git 存儲庫進行淺層克隆(例如,具有單個分支, master ),並保持最新狀態,同時保持淺層?

是的,這是可能的,以下 git 命令是 shell 腳本函數,但實際上它們可能是 bat 文件或類似文件。

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

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

# 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 s 指向 blob 的分支master之外的標簽
  • 限制到單個分支也很有用,假設您只對master或至少單個分支感興趣
  • 每次拉動后重新執行淺層似乎是一項繁重的操作,但我沒有找到更好的方法

感謝: https : //stackoverflow.com/a/7937916/432509 (對於這個答案的重要部分)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM