【问题标题】:git fetch to different local remote refspecgit fetch 到不同的本地远程 refspec
【发布时间】:2018-06-07 13:48:29
【问题描述】:

我的远程源上只有 master 分支。

然后我做了:

git fetch origin refs/heads/master:refs/remotes/origin/master2

结果我得到了:

* [new branch]      master     -> origin/master2

好像没问题。

它显示为带有master的远程跟踪分支:

bash$ git branch -r
origin/HEAD -> origin/master
origin/master
origin/master2

但master2显示为:

bash$ git remote show origin
  Remote branches:
master                      tracked
refs/remotes/origin/master2 stale (use 'git remote prune' to remove)

我。我的第一个问题是为什么 master2 显示为过时?我能够获取它(并将其创建为我的本地远程跟踪之一)并且我希望它将映射到远程源/主服务器?

二。第二个问题是为什么我必须这样做:

bash$ git branch -r -d origin/master2

通过提供完整的参考规范来删除它并在尝试执行此操作时出错:

bash$ git branch -r -d refs/remotes/origin/master2
error: remote-tracking branch 'refs/remotes/origin/master2' not found.

我检查了 git-branch 的 man,发现分支名称没有什么特别之处:

<branchname>
       The name of the branch to create or delete. The new branch name
       must pass all checks defined by git-check-ref-format(1). Some of
       these checks may restrict the characters allowed in a branch name.

【问题讨论】:

    标签: git


    【解决方案1】:

    查看我之前的回答“What is a “stale” git branch?”:您正在创建一个本地的master 分支来跟踪一个名为master2 的远程分支。
    但是master2origin 存储库中不存在。
    因此,陈旧的属性。

    fetch 不会从远程不存在的分支master2 获取任何提交,但会创建一个本地的master 分支跟踪origin/master2,这意味着下一个git push 将推送(并创建)@987654335 @。

    正如我mention here,一个简单的git fetch origin -p --progress 将删除origin/master2

    正如the OPthe comments 中提到的,“Git Internals - The Refspec”一章确实包括:

    如果您只想进行一次提取,也可以在命令行中指定特定的 refspec。
    要将远程的master 分支拉到本地的origin/mymaster,可以运行:

    $ git fetch origin master:refs/remotes/origin/mymaster
    

    【讨论】:

    • 感谢您的快速回复。据我了解,在第一次推送 master2 后将在远程创建,之后我的本地 refs/remotes/origin/master2 将失去“陈旧”状态,但 origin/master2 和 remotes/origin/master2 将是空的?
    • @user1337 是的,在第一次推送后,master2 将失去“陈旧”状态。但它不会为空:它将至少有一个来自master 的提交。
    • 我在:git-scm.com/book/en/v2/Git-Internals-The-Refspec 中找到了它,您可以在其中找到:如果您只想进行一次提取,您也可以在命令行上指定特定的 refspec。要将远程上的 master 分支拉到本地的 origin/mymaster,您可以运行: $ git fetch origin master:refs/remotes/origin/mymaster
    • @user1337 好消息:我已将您的评论包含在答案中以提高知名度。
    猜你喜欢
    • 2011-11-02
    • 2011-07-30
    • 2018-08-21
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 2013-05-26
    • 2021-03-27
    • 2011-11-28
    相关资源
    最近更新 更多