git bundle:本地和远程仓库之间的双向所有分支同步
git bundle: two-way all branches sync between local and remote repo
机器 A 有 Internet 连接,而机器 B 没有。两者都有本地存储库,机器 A 可以与 Github 交互。开发发生在两台机器上。 git-bundle 用于保持存储库同步。
通常的同步流程:
创建 B 中所有分支的捆绑并将捆绑传输到 A。
克隆 A 上的 github 存储库。将 bundle 中的所有分支拉入从 github 克隆的存储库。将更新的存储库(所有分支)推送到 github.
从 github 存储库中创建所有分支的捆绑包。将 bundle 传输到 B。将 bundle 中的所有分支拉入 B 上的存储库。
有一种方法可以创建存储库所有分支的捆绑包。但是有没有办法一次将一个包的所有分支拉到本地存储库中?
在单分支存储库的情况下,双向同步似乎很简单。
因为 bundle 就像任何其他存储库一样 - 唯一的区别是捆绑恰好存储为单个文件 - 您可以使用 git pull --all
来 fetch 束中的所有分支并将它们 合并 到它们相应的跟踪分支中:
git pull --all /path/to/bundle
但是请注意,--all
选项仅适用于 git fetch
。这意味着只有当前的本地分支(即 HEAD
引用的分支)将被更新。如果您还想更新所有本地分支,则必须自己编写脚本或使用 git-up.
之类的脚本
将以下 pullbundlebranches git 添加到您的 ~/.gitconfig
别名,然后 运行 git pullbundlebranches ../[filename].gitbundle
.
[alias]
pullbundlebranches = "!f() { git pull --tags ; git fetch ; git bundle verify | 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"
它从包中获取,然后尝试 update/create 其中包含的每个分支。如果您自己的分支版本领先或相等,则什么都不做。如果该分支与捆绑包中的版本不同,则会打印一条错误,说明如何切换到捆绑包版本,并且什么都不做。
运行ning 的示例,然后强制分支到包中的版本,然后再次重新 运行ning,但什么都不做:
$ 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
机器 A 有 Internet 连接,而机器 B 没有。两者都有本地存储库,机器 A 可以与 Github 交互。开发发生在两台机器上。 git-bundle 用于保持存储库同步。
通常的同步流程:
创建 B 中所有分支的捆绑并将捆绑传输到 A。
克隆 A 上的 github 存储库。将 bundle 中的所有分支拉入从 github 克隆的存储库。将更新的存储库(所有分支)推送到 github.
从 github 存储库中创建所有分支的捆绑包。将 bundle 传输到 B。将 bundle 中的所有分支拉入 B 上的存储库。
有一种方法可以创建存储库所有分支的捆绑包。但是有没有办法一次将一个包的所有分支拉到本地存储库中?
在单分支存储库的情况下,双向同步似乎很简单。
因为 bundle 就像任何其他存储库一样 - 唯一的区别是捆绑恰好存储为单个文件 - 您可以使用 git pull --all
来 fetch 束中的所有分支并将它们 合并 到它们相应的跟踪分支中:
git pull --all /path/to/bundle
但是请注意,--all
选项仅适用于 git fetch
。这意味着只有当前的本地分支(即 HEAD
引用的分支)将被更新。如果您还想更新所有本地分支,则必须自己编写脚本或使用 git-up.
将以下 pullbundlebranches git 添加到您的 ~/.gitconfig
别名,然后 运行 git pullbundlebranches ../[filename].gitbundle
.
[alias]
pullbundlebranches = "!f() { git pull --tags ; git fetch ; git bundle verify | 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"
它从包中获取,然后尝试 update/create 其中包含的每个分支。如果您自己的分支版本领先或相等,则什么都不做。如果该分支与捆绑包中的版本不同,则会打印一条错误,说明如何切换到捆绑包版本,并且什么都不做。
运行ning 的示例,然后强制分支到包中的版本,然后再次重新 运行ning,但什么都不做:
$ 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