使用git bundle对git仓库进行差异备份



我想知道是否可以使用git bundle对git存储库进行差异备份:

第一次:

git clone --mirror https://github.com/me/myrepo
git bundle create base.bundle --all

每次我想创建一个差异包:

cd myrepo
git fetch --all # is this necessary? (because "git bundle" doesn't check remote)
git bundle create diff.bundle $(git rev-parse HEAD)..HEAD --all

我的主要问题是,如果上述方法确保基础。bundle和diffs .bundle一起使用时,包含从创建仓库到获取diffs .bundle为止的完整仓库,包括分支、标记

前两个命令可以很好地创建基本包。然而,您的第二组命令不会做正确的事情。您确实需要git fetch(只有当您有多个远程时才需要--all),但是您希望新创建的差异包使用"负反射"完成;对于上一个bundle中为正refspec的每个ref。即:

git bundle create diff.bundle $(git rev-parse HEAD)..HEAD --all

显然是错误的,原因有二:

  1. 内部git rev-parse HEAD使用电流HEAD,可能不正确;
  2. 如果之前的bundle(初始或之前的差异)使用refs/heads/br1,refs/heads/br2,refs/tags/t1,refs/remotes/origin/r1refs/remotes/origin/r2作为通过--all的正反射规范,则需要负反射规范来从所有正反射规范中生成每个哈希ID。

解决这两个问题最简单的方法是:

  1. 初始序列以git rev-parse --all结尾,输出保存在某处;
  2. 创建一个新的差异包,对上次保存的输出中列出的每个哈希ID使用^$hash;
  3. 在创建新的差异包之后,再次使用git rev-parse --all来获得正的反射规范哈希id。

你会得到这样的结果:

git clone --mirror https://github.com/me/myrepo
git bundle create $HOME/b/base.bundle --all
git rev-list --all > $HOME/b/hashes

紧随其后:

cd myrepo
git fetch  # --all if needed, but per the above there's just one remote
git bundle create $HOME/b/diff.bundle $(sed s/^/^/ < $HOME/b/hashes) --all
git rev-list --all > $HOME/b/hashes

警告:这是完全未经测试的。我还假设每个diff.bundle都是之前的diff.bundle的增量,也就是说,这些都需要单独保存。

(你可能最好使用真正的备份软件,但这可能是可行的。)

最新更新