仅备份Git仓库中的“本地”提交/对象

3
我想备份本地存储库中的未推送提交(通过dropbox或google drive或类似服务)。备份整个存储库(使用“git push —mirror”或类似命令)是多余的,因为99%的提交已存在于中央共享存储库以及团队中所有其他开发人员的计算机上。此外,完全克隆存储库可能会超出目标备份系统的配额。
我认为我需要某种 git bundle 命令,但不确定如何将其等效地传递给“--all not in any remote refs”。
2个回答

2

这篇回答阐述了使用^<remote>/<branch>的基本原理,以排除所有可从远程分支到达的提交。

这个脚本应该能帮助你开始:

# See https://dev59.com/2Gct5IYBdhLWcg3wEJcm#18359250 for awk branch parsing.
# Get all local branch names.
LOCAL_HEADS=$(git branch | awk -F ' +' '! /\(no branch\)/ {print $2}')
# Get all remote branch names, prefixed with ^ to 'not' them.
REMOTE_HEADS=$(git branch -r | awk -F ' +' '! /\(no branch\)/ {print "^"$2}')

# -- is needed at the end to tell git we are only giving it revisions.
# List all commits reachable from local branches and tags but exclude those reachable by the remote branches.
git rev-list $REMOTE_HEADS $LOCAL_HEADS --tags --
# Pass the same args to create a bundle of all the above commits.
git bundle create my-backup-today.bundle $REMOTE_HEADS $LOCAL_HEADS --tags --

我现在用的机器和我之前用来测试上述脚本的机器不同,所以我不能确定,但是这个答案提到了--tags选项。然而,我认为那将是所有标签,而不仅仅是指向进入bundle的提交的标签。 - Henry Blyth
阅读了rev-list的手册后,--tags将包括所有标签作为提交。你可以用--tags=<pattern>来限制,但是没有必要,因为你想添加除$REMOTE_HEADS以外的所有提交。更新了答案以包括标签。 - Henry Blyth

0

除非你的存储库非常庞大,否则我不确定绕过git push会有什么好处。

一旦你第一次完成它,你的push就变成了增量式的,并且从你的私有远程获取数据非常容易。你的新的private-remote/*远程分支可以直观地显示哪些已经被push了,哪些没有。

使用git bundle会使你的工作流程更加复杂,并且你会失去备份哪些提交的可见性。


我会定期在后台安排这些备份,因此可见性并不是必要的(甚至可能会分散注意力)。 - Matt McHenry

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接