【问题标题】:Git: which is the default configured remote for branch?Git:哪个是分支的默认配置远程?
【发布时间】:2011-06-18 08:10:23
【问题描述】:

我有一个远程裸存储库hub。我只在master 分支工作。 下面这条错误信息的最后一句让我想知道:我如何找出“当前分支的默认配置远程”?以及如何设置?

[myserver]~/progs $ git remote -v
hub     ~/sitehub/progs.git/ (fetch)
hub     ~/sitehub/progs.git/ (push)

[myserver]~/progs $ git branch -r
  hub/master

[myserver]~/progs $ cat .git/HEAD
ref: refs/heads/master

[myserver]~/progs $ git pull hub
You asked to pull from the remote 'hub', but did not specify
a branch. Because this is not the default configured remote
for your current branch, you must specify a branch on the command line.

【问题讨论】:

    标签: git git-push remote-branch


    【解决方案1】:

    您可以更简单地做到这一点,保证您的.gitconfig 处于有意义的状态:

    使用Git v1.8.0及以上版本

    git push -u hub master 推送时,或:
    git branch -u hub/master

    或

    (这会将当前签出分支的远程设置为hub/master)
    git branch --set-upstream-to hub/master

    或

    (这会将名为 branch_name 的分支的远程设置为 hub/master)
    git branch branch_name --set-upstream-to hub/master

    如果您使用的是v1.7.x 或更早版本

    你必须使用--set-upstream:
    git branch --set-upstream master hub/master

    【讨论】:

    • 对于任何想知道的人:第二个命令可用于现有分支
    • @eric-hu 如我在此处的回答中所述:stackoverflow.com/questions/4878249/…
    • set-upstream[-to] 命令更改当前配置的远程。原始发帖人询问了 default 配置的遥控器。这肯定不是同一个概念吗?
    • 每个分支都有一个当前配置的远程,指定该远程上的哪个分支对应于本地分支。如果您没有明确指定一个分支,则 default 配置的远程会确定推送或拉出哪个分支。此答案仅设置 current 之一。接受的答案(手动编辑)还允许您设置 default 之一。有谁知道避免手动编辑的命令?
    • --set-upstream-to 在.git/config 中所做的更改与@scragz 在his answer 中建议的完全相同。
    【解决方案2】:

    跟踪远程分支

    您可以使用 git-branch 的 track 选项指定用于推送和拉取的默认远程存储库。您通常可以通过在创建本地 master 分支时指定 --track 选项来执行此操作,但由于它已经存在,我们将手动更新配置,如下所示:

    编辑您的.git/config

    [branch "master"]
      remote = origin
      merge = refs/heads/master
    

    现在你可以简单地 git push 和 git pull。

    [source]

    【讨论】:

    • 如果 OP 做了git pull hub master,这不会也设置吗?
    • 为什么在 git 命令存在的时候编辑配置文件?
    • git branch --set-upstream local_branch remote/remote_branch(或推送时,如下详述)
    • @scragz:不可能!命令方法可确保您的 .gitconfig 处于有意义的状态。
    • 我使用这种方法的原因是我有一堆分支,因此与为每个分支应用单独的命令相比,我花费的时间更少。
    【解决方案3】:

    为了完整起见:前面的答案告诉了如何设置上游分支,而不是如何查看它。

    有几种方法可以做到这一点:

    git branch -vv 显示所有分支的信息。 (在大多数终端中以蓝色格式化)

    cat .git/config 也显示了这一点。

    供参考:

    【讨论】:

      【解决方案4】:

      这个问题的答案的程序化版本是:

      git branch --show-current -vv --format='%(upstream:remotename)'
      

      这将只输出当前分支的默认远程名称。 --show-current 选项在 Git 版本 2.22.0 之前将不起作用。

      【讨论】:

        【解决方案5】:

        获取分支的有效推送远程的命令,例如,master,是:

        git config branch.master.pushRemote || git config remote.pushDefault || git config branch.master.remote

        原因如下(来自“man git config”输出):

        branch.name.remote [...] 告诉 git fetch 和 git push 从哪个远程获取/推送到 [...] [for push]可以用 remote.pushDefault 覆盖(对于所有分支)[和] 对于当前分支 [..] 进一步被 branch.name.pushRemote [...]

        出于某种原因,“man git push”只讲述了 branch.name.remote(即使它的优先级是三个中最低的)+ 错误地指出,如果未设置,则 push 默认为 origin - 它没有,只是当你克隆一个repo时,branch.name.remote被设置为origin,但是如果你去掉这个设置,git push会失败,即使你还有origin remote

        【讨论】:

        • 这个问题已经得到了更有用的答案。
        猜你喜欢
        • 2022-01-09
        • 2021-05-06
        • 2011-02-24
        • 2011-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-04
        • 2016-07-11
        相关资源
        最近更新 更多