git clone --reference,但带有硬链接

git clone --reference, but with hardlinks

我喜欢 git clone --reference 节省网络容量和磁盘 space 的方式。但是,如果删除参考仓库,新的仓库就会损坏。

据我了解 --reference 使用 .git/objects/info/alternates 共享对象。但是,如果它使用硬链接代替,就像 --local 那样,那么就不会有这样的问题——它可以节省网络容量、磁盘 space,并且在删除参考存储库时不会损坏.

有没有办法达到这样的效果?

我编写了以下脚本来实现这一点。第一次是 运行 它将创建一个缓存仓库,它是远程仓库的镜像。因此它将使用这个缓存来制作一个 --local 克隆(具有硬链接和节省网络容量的所有好处),然后它覆盖 origin 成为远程仓库,而不是本地缓存仓库.

#!/usr/bin/env bash
#
# Usage: clone <branch> [<target-directory-name>]
#
set -ueo pipefail

readonly BRANCH=
readonly TARGET=${2:-$BRANCH}

readonly ORIGIN_REPO='git@...'
readonly CACHE_REPO='/tmp/cache-repo.git'

if [[ -e "$CACHE_REPO" ]]; then
  echo "Updating existing cache repo at: $CACHE_REPO"
  git --git-dir="$CACHE_REPO" remote update --prune
else
  echo "Creating cache repo from scratch"
  git clone --bare --mirror "$ORIGIN_REPO" "$CACHE_REPO"
fi

git clone --local "$CACHE_REPO" "$TARGET" --branch="$BRANCH"
git --git-dir="$TARGET/.git" remote set-url origin "$ORIGIN_REPO"