【问题标题】:Transfer for branch to anotherRemote/branch except origin remote with Git使用 Git 将分支转移到 anotherRemote/branch,除了 origin 远程
【发布时间】:2021-08-15 15:18:17
【问题描述】:

我有两个遥控器。一个是origin,另一个是另一个Remote。还有这两个遥控器的网址。我的目的是将代码从 origin/master 转移到 anotherRemote 中的 test 分支。

我的交易。

$ 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 github branch git-pull git-remote


    【解决方案1】:

    你可以试试:

    git push <remote> <local_branch>:<remote_name>
    

    在你的情况下是:

    $ git push anotherRemote HEAD:testBranch
    

    查看this answer了解更多详情

    【讨论】:

    • 这只有在 HEAD 解析到正确的提交哈希 ID 时才有效。在 OP 运行此 git push 命令时可能会或可能不会出现这种情况。
    • 感谢您的回答。我推送了一个现有的 Git 存储库。这是我的错造成的问题。但我也在这里学到了新东西。
    【解决方案2】:

    这个命令:

    git push anotherRemote testBranch
    

    是:

    git push anotherRemote testBranch:testBranch
    

    也就是说,您的 Git 将:

    1. testBranch:testBranch 的左侧查找提交哈希ID。
    2. 假设第 1 步有效,请通过存储在名称 anotherRemote 下的 URL 联系回答问题的 Git,并发送带有该哈希 ID 的提交。
    3. 假设第 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/masterrefs/remotes/origin/master 的缩写。

    当您运行 git branch -r 时,您的 Git 会显示您的远程跟踪名称,而 refs/remotes/ 会从前面剥离。当你运行git branch -r 时,你的Git 会显示你的远程跟踪名称,只去掉前面的refs/,这样你就会看到remotes/origin/master 而不是origin/master全名仍然是refs/remotes/origin/master:改变的是Git去掉了多少这个无聊的全名,让这个名字看起来更令人兴奋。

    【讨论】:

    • 感谢您的回答。我推送了一个现有的 Git 存储库。这是我的错造成的问题。但我也在这里学到了新东西。谢谢。问题解决了。我试过你的建议。它有效。
    猜你喜欢
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 2016-04-25
    • 2016-06-14
    • 1970-01-01
    • 2019-05-09
    • 1970-01-01
    • 2011-10-10
    相关资源
    最近更新 更多