如何解决使用 Git 将分支转移到另一个远程/分支,除了 origin 远程
我有两个遥控器。一个是起源,另一个是另一个远程。还有这两个遥控器的网址。我的目的是将代码从 origin/master 转移到另一个远程的测试分支。
我的交易。
$ git remote add anotherRemote <anotherRemote-url>
$ git remote -v
$ anotherRemote <anotherRemote-url> (fetch)
$ anotherRemote <anotherRemote-url> (push)
$ origin <origin-url> (fetch)
$ origin <origin-url> (push)
$ git fetch anotherRemote
$ git add .
$ git commit -m "initial commit"
$ git push anotherRemote testBranch
但我收到以下错误
error: src refspec testBranch does not match any
error: Failed to push some refs to '<anotherRemote-url>'
解决方法
你可以试试:
git push <remote> <local_branch>:<remote_name>
在您的情况下:
$ git push anotherRemote HEAD:testBranch
查看this answer了解更多详情
,这个命令:
git push anotherRemote testBranch
缩写为:
git push anotherRemote testBranch:testBranch
也就是说,您的 Git 将:
- 在
testBranch:testBranch
的左侧查找提交哈希 ID。 - 假设第 1 步有效,请联系在名称
anotherRemote
下存储的 URL 上回答的 Git,并发送与该哈希 ID 对应的提交。 - 假设第 2 步有效,请其他 Git 将它的分支名称
testBranch
设置为第 1 步中找到的提交哈希 ID。
但是您没有名为 testBranch
的分支,因此第 1 步会失败。 (您可以创建这个分支名称,但您不必这样做。)
我的目的是将代码从 origin/master
转移到 anotherRemote 中的测试分支。
名称 origin/master
,在您自己的 Git 存储库中,是您的 Git 查找特定提交的哈希 ID 的一种方式。1 此名称可用于左侧source:destination
的 git push
语法。不过有一个小故障。因此,您可能希望使用:
git push anotherRemote origin/master:refs/heads/testBranch
这会调用与上面相同的三步过程,但不是尝试在您自己的存储库中查找名称 testBranch
,而是使用您自己的存储库中的名称 origin/master
来查找正确的提交哈希身份证。
请注意,右手边现在是 refs/heads/testBranch
而不是 testBranch
。这是分支名称 testBranch
的完整拼写。我们现在必须使用完整拼写而不是简单的缩写的原因是git push
不再知道我们要使用分支名称。例如,我们可以在 anotherRemote
处请 Git 创建一个标签名称。
使用像这样的完整拼写告诉我们的 Git,我们希望它要求另一个 Git 创建一个分支名称。没有这个,我们得到以下内容:
$ git push origin origin/xyz:newbranch
error: The destination you provided is not a full refname (i.e.,starting with "refs/"). We tried to guess what you meant by:
- Looking for a ref that matches 'newbranch' on the remote side.
- Checking if the <src> being pushed ('refs/remotes/origin/xyz')
is a ref in "refs/{heads,tags}/". If so we add a corresponding
refs/{heads,tags}/ prefix on the remote side.
Neither worked,so we gave up. You must fully qualify the ref.
hint: The <src> part of the refspec is a commit object.
hint: Did you mean to create a new branch by pushing to
hint: 'refs/remotes/origin/xyz:refs/heads/newbranch'?
error: failed to push some refs to <url>
这条很长的错误消息是 Git 的说法:请在此处使用 refs/heads/newbranch
。
1您的 Git 在此处找到的提交哈希 ID 取决于您上次在 origin
与 Git 对话的时间。当您运行 git fetch origin
时,您的 Git 会调用它们的 Git。他们的 Git 列出了他们的分支名称和与每个名称对应的提交哈希 ID。然后,您的 Git 会获取这些提交(如果您还没有这些提交),然后根据它们的分支名称更新您的 origin/*
名称。
事实上,您的 Git 根据他们的 origin/master
(分支名称)更新了您的 master
(远程跟踪名称),这就是为什么许多人称这些为远程- 跟踪分支名称。不过,它们不是分支名称。 分支名称在内部以 refs/heads/
开头,这些远程跟踪名称的完整拼写以 refs/remotes/
开头。也就是说,例如,您的 master
分支是 refs/heads/master
的缩写;您的 origin/master
是 refs/remotes/origin/master
的缩写。
当您运行 git branch -r
时,您的 Git 会显示您的远程跟踪名称,并去掉前面的 refs/remotes/
。当您运行 git branch -r
时,您的 Git 会显示您的远程跟踪名称,仅去掉前面的 refs/
,因此您将看到 remotes/origin/master
而不是 origin/master
。 全名仍然是refs/remotes/origin/master
:改变的是这个无聊的全名Git去掉了多少让这个名字看起来更令人兴奋。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。