为了避免过多的网络流量和加快用户的git操作,我们设置了以下只读git镜像:
$> git remote -v
origin ssh://git@local.mirror.company.com/repo.git (fetch)
origin ssh://git@central.server.over.slownetwork.company.com/repo.git (push)而且效果很好。
然而,要推送,您的本地回购必须是最新的和延迟镜像从中央服务器到本地镜像可能意味着,尽管你做了一个git pull,你不是最新的中央服务器。即使镜像延迟只有12秒,如果有人在错误的时间尝试git pull && git push,他们也会感到非常沮丧。
问题:,如果他们能git pull --pull-from-push-url,那么问题就根本没有了。有什么办法可以做到这一点吗?
(是的,你可以运行git fetch <url of --push server>,但我不认为这真的有帮助。我认为它不会更新您的提交、合并/重定向或更新remotes/origin/<branch> ref之类的内容。)
更新
基于@jthill的响应,我在脚本的fetch部分实现了以下内容,只获取当前的分支以加快速度,并允许使用没有push url的远程处理。
git fetch "$(git config remote.origin.pushurl || git config remote.origin.url)" \
"$(git rev-parse --symbolic-full-name --abbrev-ref @{upstream} |
sed -r -e 's=^origin/(.*)=+refs/heads/\1:refs/remotes/origin/\1=')"发布于 2021-11-25 17:22:33
是的,你能做到的。Fetch以"refspecs“为基础,说明哪些源提交(通常是由一个refname模式标识)发送,以及(可选,但通常)给它们提供哪些目标重构名。
普通克隆的工厂默认重构规范是
+refs/heads/*:refs/remotes/origin/*这意味着我们还没有源提交,在源中获取与refs/heads/名称标识的重构名,并给出用refs/remotes/origin/名称标识的目标名称;+意味着您不关心是否更新目标引用放弃历史记录(因为它们是远程跟踪参考)。
现在,事情是这样的:遥控器只是一种方便,它是一种记忆您喜欢如何进行抓取和推送的方式,而git pull只是在抓取之后做您喜欢做的事情的一种方便。
Remotes和config选项等并不能反映作为选项的底层机器的每一种可能的使用,抽象对于照亮和组织您对领土的看法是很好的,但它们是路灯。他们不可能把这一切都掩盖起来。Git的方法是提供一些方便,以便自动选择通常的情况、选择它们的选项,以及填充通常细节的配置项;对于角落中的情况,即奇怪的情况,您只需直接使用核心命令--这就是方便命令所做的。它们一开始都是调用核心命令的shell脚本,但仍然可以以这种方式实现,只是速度会稍微慢一些(有时会不知不觉地)。
git fetch # get up to date with the local mirror
git fetch $( # get up to date with the real remote
git config remote.origin.pushurl) $(
git config remote.origin.fetch)
git merge # or git rebase, whatever it is you like to do after fetching发布于 2021-11-25 17:22:26
您可以将中央回购和镜像设置为两个独立的遥控器,然后使用一个用于分支的remote设置,另一个用于pushRemote设置。
下面是在.git/config中的样子
[remote "mirror"]
url = git@mirror.example:repo/
fetch = +refs/heads/*:refs/remotes/mirror/*
[remote "central"]
url = git@central.example:repo/
fetch = +refs/heads/*:refs/remotes/central/*
[branch "master"]
remote = origin
pushRemote = origin-central
merge = refs/heads/master`这样,如果由于镜像上过时的分支指针而导致冲突,则始终可以从中央服务器获取并在中心服务器的分支之上重新设置提交。
您甚至可以设置两个遥控器来更新常见的远程跟踪分支指针:
[remote "mirror"]
url = git@mirror.example:repo/
fetch = refs/heads/*:refs/remotes/origin/*
fetch = +refs/heads/*:refs/remotes/mirror/*
[remote "central"]
url = git@central.example:repo/
fetch = +refs/heads/*:refs/remotes/origin/*
fetch = +refs/heads/*:refs/remotes/central/*镜像设置中省略了+前缀,以防止来自过时镜像的获取向后更新分支指针(即非快速转发更新)。
必须为每个分支分别设置pushRemote设置。另一种混合解决方案是设置中央遥控器以更新与镜像遥控器相同的远程跟踪分支指针:
[remote "origin"]
url = git@mirror.example:repo/
pushurl = git@central.example:repo/
fetch = refs/heads/*:refs/remotes/origin/*
[remote "origin-central"]
url = git@central.example:repo/
fetch = +refs/heads/*:refs/remotes/origin/*这避免了为每个分支设置pushRemote。
https://stackoverflow.com/questions/70114436
复制相似问题