git bundle:本地和远程repo之间的双向所有分支同步

时间:2016-06-29 11:16:28

标签: git github git-bundle

机器A具有Internet连接,而机器B没有。两者都有本地存储库,机器A可以与Github交互。两台机器都在进行开发。 git-bundle用于保持存储库同步。

通常的同步流程:

  1. 在B中创建一个包含所有分支的捆绑包,并将捆绑包转移到A.

  2. 在A上克隆github存储库。将所有分支从bundle中拉入从github克隆的存储库。将更新的存储库(所有分支)推送到github。

  3. 从github存储库创建所有分支的捆绑包。将捆绑包转移到B.将捆绑包​​中的所有分支拉到B上的存储库中。

  4. 有一种方法可以创建存储库的所有分支的捆绑。但有没有办法将捆绑包的所有分支同时拉入本地存储库?

    对于单个分支存储库,双向同步似乎是直接的。

2 个答案:

答案 0 :(得分:2)

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

git pull --all /path/to/bundle

但请注意,--all选项仅适用于git fetch。这意味着只会更新当前本地分支(即HEAD引用的分支)。如果您还想更新所有本地分支机构,您必须自己编写脚本或使用git-up之类的内容。

答案 1 :(得分:0)

在您的~/.gitconfig中添加以下pullbundlebranches git别名,然后运行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