简体   繁体   中英

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

There are some cases where its not useful to store the entire git history (a build-bot for example).

Is it possible to do a shallow clone of a git repository (with a single branch, master for example), and keep it up-to-date, while staying shallow?

Yes this is possible, the following git-commands are shell-script functions, but really they could be bat files or similar.

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

Notes

  • --no-tags is important to use, otherwise you may clone tags which have sha1 s pointing to blob's outside of the branch master
  • restricting to a single branch is also useful, assuming you're only interested in master , or a single branch at least
  • re-enforcing shallow after every pull seems quite a heavy operation, but I didn't find a better way

Thanks to: https://stackoverflow.com/a/7937916/432509 (for the important part of this answer)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM