gpt4 book ai didi

Git:--force-with-lease 和多个 pushurls

转载 作者:IT王子 更新时间:2023-10-29 01:04:21 30 4
gpt4 key购买 nike

我有一个带有远程 origin 的 git 仓库镜像在 3 台主机上。

$ git remote -v
origin git@github.com:username/repo.git (fetch)
origin git@github.com:username/repo.git (push)
origin git@gitlab.com:username/repo.git (push)
origin git@bitbucket.org:username/repo.git (push)

任何地方的一切都在提交 A

$ git rev-parse HEAD
A

$ cat .git/refs/remotes/origin/master
A

我提交了 B 并推送了它。现在每个人都在提交 B

$ git push origin master
To github.com:username/repo.git
A...B master -> master
To gitlab.com:username/repo.git
A...B master -> master
To bitbucket.org:username/repo.git
A...B master -> master

$ git rev-parse HEAD
B

$ cat .git/refs/remotes/origin/master
B

现在我注意到最后一次提交中有一个错误,所以我修复了它并修改了提交。这使我与 Remote 不同步。

$ git rev-parse HEAD
C

$ cat .git/refs/remotes/origin/master
B

我喜欢盲目回避--force推,所以我使用 --force-with-lease ,但这以一种有趣的方式失败了。

$ git push --force-with-lease origin master
To github.com:username/repo.git
+ B...C master -> master (forced update)
To gitlab.com:username/repo.git
! [rejected] master -> master (stale info)
To bitbucket.org:username/repo.git
! [rejected] master -> master (stale info)

问题是,--force-with-lease只有当远程 ref 与我上次与其通信时处于同一提交时,才会认为推送是安全的,并且我的本地记录了该提交的 sha1 .git/refs/remotes/origin/master .一旦第一个镜像 (GitHub) 更新,git 更新我本地的远程引用以提交 C,导致对 GitLab 和 Bitbucket 的推送尝试失败,因为我们现在期望它们位于提交C

我想解决这个问题,所以首先我强制 GitHub 镜像返回提交 B

$ git push origin +B:refs/heads/master
To github.com:username/repo.git
+ C...B B -> master (forced update)
Everything up-to-date
Everything up-to-date

现在我需要更具体地说明我希望 Remote 在哪个提交上进行推送。该文档说您可以准确指定要更新的引用,以及您希望它当前在 --force-with-lease=<refname>:<expect> 处的提交。 , 所以我试试看。

$ git push --force-with-lease=origin/master:B origin master
To github.com:username/repo.git
! [rejected] master -> master (non-fast-forward)
To gitlab.com:username/repo.git
! [rejected] master -> master (non-fast-forward)
To bitbucket.org:username/repo.git
! [rejected] master -> master (non-fast-forward)

显然我做错了什么。也许我有 <refname>错误的?我觉得我很亲近。我错过了什么?

最佳答案

很有意思的问题!我已经尝试过并在这些测试 repo 中成功地重现了您使用 Git 2.11.0 描述的内容:

我能够使用 --force-with-lease 成功推送使用以下形式发送到两个远程 URL:

git push --force-with-lease origin +master

注意 +在分行名称前签名。这是输出:

$ git push --force-with-lease origin +master
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 232 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To github.com:hkdobrev/git-test.git
+ 099b95f...08c7548 master -> master (forced update)
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 232 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To github.com:hkdobrev/git-test2.git
+ 099b95f...08c7548 master -> master (forced update)

来自git-push (1)手册页:

Note that --force applies to all the refs that are pushed, hence using it with push.default set to matching or with multiple push destinations configured with remote.*.push may overwrite refs other than the current branch (including local refs that are strictly behind their remote counterpart). To force a push to only one branch, use a + in front of the refspec to push (e.g git push origin +master to force a push to the master branch). See the <refspec>... section above for details.

关于Git:--force-with-lease 和多个 pushurls,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39693153/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com