git bundle:本地与远程仓库之间双向同步所有分支

4

机器A有互联网连接,而机器B没有。它们都有本地仓库,机器A可以与Github交互。开发在两台机器上进行。使用git-bundle来保持仓库同步。

同步的常规流程:

  1. 创建B中所有分支的bundle并将其传输到A。

  2. 在A上克隆github仓库。从bundle中拉取所有分支到从github克隆的仓库中。将更新后的仓库(所有分支)推送到Github。

  3. 创建github仓库所有分支的bundle。将bundle传输到B。从bundle中拉取所有分支到B上的仓库中。

有一种方法可以创建一个包含仓库所有分支的bundle。但是是否有一种方式可以一次性将bundle中的所有分支拉取到本地仓库中?

对于单分支仓库,双向同步似乎很简单。


2
B是否可以访问A的网络,还是它们在网络上完全独立? - Vampire
如果吸血鬼的问题的答案是肯定的,那么你可以直接在A和B之间进行推拉操作,而无需使用互联网。否则,我猜想你正在使用闪存驱动器或物理媒介来传输捆绑包? - Jeff Puckett
@Vampire:不是。B是一座孤岛。 - subba
@JeffPuckettII:我正在使用闪存驱动器传输捆绑文件。 - subba
2
@subba,那么Enrico Campidoglio的答案应该是正确的。 - Vampire
2个回答

1

由于bundle就像任何其他存储库一样 - 唯一的区别是bundle恰好被存储为单个文件 - 您可以使用git pull --all从bundle中获取所有分支,并将它们合并到相应的跟踪分支中:

git pull --all /path/to/bundle

请注意,--all选项仅适用于git fetch。这意味着只有当前的本地分支(即由HEAD引用的分支)将被更新。如果您想要更新所有本地分支,您需要编写一个脚本或使用类似git-up的工具。

2
这个答案是不正确的;给定的命令会产生“fatal: fetch --all does not take a repository argument”的错误。 - TamaMcGlinn
注意:我没有输错命令。Git的错误信息确实是错误的 - 我们发出了pull --all,但错误消息说fetch --all不接受存储库。我正在使用git版本2.23.0.windows.1。 - TamaMcGlinn

1
将以下pullbundlebranches git别名添加到您的~/.gitconfig文件中,然后运行git pullbundlebranches ../[filename].gitbundle命令。
[alias]
  pullbundlebranches = "!f() { git pull --tags $1; git fetch $1; git bundle verify $1 | grep ' refs/heads/' | (while read line; do \
    commit=`echo $line | cut -d' ' -f1`; branch=`echo $line | sed 's_[^\\ ]*\\ refs/heads/__'`; \
    if git show-ref -q --heads $branch; then \
      old_commit=`git rev-parse $branch`; \
      if [ \"$old_commit\" = \"$commit\" ]; then \
        echo 'Skipping' $branch 'which is up-to-date at' $old_commit; \
      elif git merge-base --is-ancestor $branch $commit; then \
        current_branch=`git rev-parse --abbrev-ref HEAD`; \
        if [ \"$current_branch\" = \"$branch\" ]; then \
          git reset --hard $commit; \
        else \
          git branch -Dq $branch; git branch $branch $commit; \
        fi; \
        echo 'Updated' $branch 'from' $old_commit 'to' $commit; \
      elif git merge-base --is-ancestor $commit $branch; then \
        echo 'Skipping' $branch 'which is ahead of bundle version ('$commit')'; \
      else \
        echo 'Error:' $branch 'already exists and diverges from upstream found in bundle'; \
        echo 'You could switch to the bundle version as follows, but you might lose work.'; \
        echo 'git checkout -B' $branch $commit; \
      fi; \
    else \
      git branch $branch $commit; \
      echo 'Created' $branch 'pointing at' $commit; \
    fi; done); }; f"

它从捆绑包中获取数据,然后尝试更新/创建其中包含的每个分支。如果您自己的分支版本领先或相等,则不执行任何操作。如果该分支与捆绑包中的版本发生了分歧,则会打印错误消息说明如何切换到捆绑包版本,并且不执行任何操作。
运行示例,然后强制将分支切换到捆绑包版本,再次运行不会有任何操作:
$ git pullbundlebranches ../../bundles/Jabberwocky_November_snapshot.gitbundle
From ../../bundles/Jabberwocky_November_snapshot.gitbundle
 * branch              HEAD       -> FETCH_HEAD
../../bundles/Jabberwocky_November_snapshot.gitbundle is okay
Error: develop already exists and diverges from upstream found in bundle
You could switch to the bundle version as follows, but you might lose work.
git checkout -B develop 6c5214a7bd9b10d5f9e49ab9eadaa1533867ebb7
Created feature/all_glory_to_him pointing at 645152be25e0e5d3eb80615c9173e88714b23ade
Created feature/praise_the_lord pointing at 6c5214a7bd9b10d5f9e49ab9eadaa1533867ebb7
Created feature/why_are_you_reading_the_branch_names pointing at a55f5f74d6b129d173770e91c5a0ffe8ff981e8e

$ git checkout -B develop 6c5214a7bd9b10d5f9e49ab9eadaa1533867ebb7
Reset branch 'develop'
Your branch is up to date with 'origin/develop'.

$ git pullbundlebranches ../../bundles/Jabberwocky_November_snapshot.gitbundle
From ../../bundles/Jabberwocky_November_snapshot.gitbundle
 * branch              HEAD       -> FETCH_HEAD
../../bundles/Jabberwocky_November_snapshot.gitbundle is okay
Skipping develop which is up-to-date at 6c5214a7bd9b10d5f9e49ab9eadaa1533867ebb7
Skipping feature/all_glory_to_him which is up-to-date at 645152be25e0e5d3eb80615c9173e88714b23ade
Skipping feature/praise_the_lord which is up-to-date at 6c5214a7bd9b10d5f9e49ab9eadaa1533867ebb7
Skipping feature/why_are_you_reading_the_branch_names which is up-to-date at a55f5f74d6b129d173770e91c5a0ffe8ff981e8e

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