Git:这是分支的默认配置远程?

leo*_*loy 190 git git-push remote-branch

我有一个远程裸存储库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.
Run Code Online (Sandbox Code Playgroud)

urs*_*rei 258

您可以更简单地做到这一点,保证您.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_namehub/master)
git branch branch_name --set-upstream-to hub/master

如果您正在使用v1.7.x或更早

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

  • 对于任何想知道的人:第二个命令可用于现有分支 (3认同)
  • `set-upstream[-to]` 命令更改**当前**配置的远程。原始发帖者询问了**默认**配置的遥控器。这肯定不是完全相同的概念吗? (2认同)

scr*_*agz 226

跟踪远程分支

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

编辑你的 .git/config

[branch "master"]
  remote = origin
  merge = refs/heads/master
Run Code Online (Sandbox Code Playgroud)

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

[ 来源 ]

  • git branch --set-upstream local_branch remote/remote_branch(或推送时,如下所述) (43认同)
  • 为什么在git命令存在时编辑配置文件? (35认同)
  • @scragz:没办法!命令方法可以保证.gitconfig处于有意义的状态. (24认同)
  • 我使用这种方法的原因是我有很多分支,因此比为每个分支应用单独的命令花费的时间更少。 (2认同)

leo*_*loy 25

为了完整起见:前面的答案说明了如何设置上游分支,而不是如何设置它.

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

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

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

以供参考: