如果我从源头 (即 repo1) 获取我的克隆库,一切都很好。但是如果我需要使用 repo2,我无法让它运行!简单地说,命令是:
git fetch --all repo2
给我这样的提示:fetch --all不接受仓库参数。
此时,我不知道该如何继续操作了。
我发现了很多解决方法,比如:如何在Git中克隆所有远程分支?
但是没有一个答案处理多个存储库!
这是否根本无法管理?
git fetch --all repo2
给我这样的提示:fetch --all不接受仓库参数。
此时,我不知道该如何继续操作了。
我发现了很多解决方法,比如:如何在Git中克隆所有远程分支?
但是没有一个答案处理多个存储库!
这是否根本无法管理?
git remote -v
检查您的存储库URL。接下来,假设您有两个名为origin
和repo2
的远程存储库。git fetch repo2
足以在本地获取远程repo2
的整个历史记录。git fetch --all
(不需要额外的参数)代替git fetch origin; git fetch repo2
。repo2
的远程对应的远程分支,只需执行git branch -r
,它将显示类似于:origin/HEAD -> origin/master
origin/master
origin/name-of-an-origin-branch
...
repo2/HEAD -> repo2/master
repo2/master
repo2/name-of-a-repo2-branch
...
gitk --all
来显示所有分支历史记录的只读摘要,或者执行git checkout name-of-a-repo2-branch
来创建与跟踪分支name-of-a-repo2-branch
关联的本地分支(请参阅git checkout文档)。git merge origin/master
),变基(例如,git rebase repo2/master
),涉及您的远程存储库的跟踪分支。git init --bare /path/to/bare-repo.git
git remote add sync file:///path/to/bare-repo.git
git config alias.pull-ff-sync '!f(){ git fetch sync && git for-each-ref --format "%(refname:strip=3)" refs/remotes/sync/ | grep -v -e '\''^HEAD$'\'' | xargs -n1 bash -c '\''git checkout -q "$1" && git pull --ff-only -v sync "$1"'\'' bash; }; f'
git config alias.pull-ff-sync # to check the alias has been properly setup
# and in particular that the «'\''» have been replaced with single «'»
git config push.default matching
for b in master develop ... ; do git push sync ${b}; done
# push all the branches you want to sync
当然,在repo2中也有类似的设置:
git remote add sync file:///path/that/can/be/different/to/bare-repo.git
git config alias.pull-ff-sync '!f(){ git fetch sync && git for-each-ref --format "%(refname:strip=3)" refs/remotes/sync/ | grep -v -e '\''^HEAD$'\'' | xargs -n1 bash -c '\''git checkout -q "$1" && git pull --ff-only -v sync "$1"'\'' bash; }; f'
git config push.default matching
to fetch all the branches you have selected and merge them if the operation is fast-forward (so this command is more powerful than just git fetch sync
because all local branches will be updated if a manual merge commit is not required):
git pull-ff-sync
to push all the branches you have selected (which relies on the matching
setting of the push.default configuration):
git push -v sync
请注意,如果您不想更改两个仓库的push.default
配置(例如,如果您还想使用git push
仅将repo1的一个分支推送到另一个远程仓库...),您可以选择:
replace
git config push.default matching
with
git config push.default upstream # recommended
or just with
git config push.default simple # the default config in Git >= 2.0
run the slightly longer command below to send all matching branches from repo1 (resp. repo2) to the sync remote in one go:
git push -v sync :
git push
,除非您已设置当前分支的上游远程,否则不会推送任何内容。但是,如果您在本地和repo2
远程都有一个master
分支,则例如git push repo2 master
应该可以工作。如果需要,我可以在我的答案中提供更多关于这个主题的指针。 - ErikMDgit fetch
, 这个我在我的答案中已经讲过了。 - ErikMDErikMD提出的两个方案是正确的。
但是,如果您不介意强制推送仓库,即您已经运行了(git pull --all
或git fetch -all
),并且确信在执行此操作期间另一台电脑不会提交任何内容,则可以这样做:
git push --mirror
来自文档:
--mirror
不需要为每个要推送的引用命名,而是指定将 refs/(其中包括但不限于 refs/heads/、refs/remotes/ 和 refs/tags/)下的所有引用镜像到远程仓库。新创建的本地引用将被推送到远程端,本地更新的引用将被强制更新到远程端,并且删除的引用将从远程端删除。如果配置选项 remote..mirror 已设置,则这是默认设置。
修复 all
选项的另一种方法是尝试 this answer。