这个命令:
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 转移到另一个远程的测试分支。
名称origin/master,在您自己的 Git 存储库中,是您的 Git 查找特定提交的哈希 ID 的一种方式。1 此名称可用于<em>source</em>:<em>destination</em> git push 的语法。不过有一个小故障。因此,您可能希望使用:
git push anotherRemote origin/master:refs/heads/testBranch
这会调用上述相同的三步过程,但不是尝试在您自己的存储库中查找名称 testBranch,而是使用您自己的存储库中的名称 origin/master 来查找正确的提交哈希 ID。
请注意,右手边现在是refs/heads/testBranch,而不仅仅是testBranch。这是分支名称testBranch 的完整拼写。我们现在必须使用完整拼写而不是简单缩写的原因是git push 不再知道我们要使用分支名称。例如,我们可以让 Git 在anotherRemote 创建一个 tag 名称。
使用这样的完整拼写告诉我们的 Git,我们希望它请求另一个 Git 创建一个 branch 名称。没有这个,我们得到以下结果:
$ 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 取决于 您上次让 Git 与 origin 的 Git 对话。当你运行git fetch origin 时,你的 Git 会调用他们的 Git。他们的 Git 列出了他们的分支名称和每个名称附带的提交哈希 ID。如果您还没有这些提交,您的 Git 然后会获取这些提交,然后根据它们的 分支 名称更新您的 origin/* 名称。
您的 Git 会根据 他们的 master(分支名称)更新您的 origin/master(远程跟踪名称),这就是为什么许多人称这些 远程跟踪分支名称。不过,它们不是 branch 名称。 分支名称在内部以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去掉了多少这个无聊的全名,让这个名字看起来更令人兴奋。