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

问题描述 投票:0回答:7

我有一个远程裸存储库

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
7个回答
267
投票

您可以做得更简单,保证您的

.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


237
投票

跟踪远程分支

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

编辑您的

.git/config

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

现在您可以简单地进行 git push 和 git pull 。

[来源]


37
投票

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

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

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

cat .git/config
也表明了这一点。

供参考:


2
投票

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

git branch --list "$(git branch --show-current)" "--format=%(upstream:remotename)"

这将仅输出当前分支的默认远程名称。在 Git 版本 2.22.0 之前,

--show-current
选项将不起作用。


2
投票

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

“获取”命令

对于名为

branch_name
的分支,用以下命令读出它:

git config branch.branch_name.remote

示例:

# main branch
git config branch.main.remote
# master branch
git config branch.master.remote

输出示例:

origin

“设置”命令

更改分支的默认远程,如下所示:

# for branch "branch_name", change the default remote to "remote_name"
git config branch.branch_name.remote remote_name

# Examples:
# main -> origin
git config branch.main.remote origin
# main -> upstream
git config branch.main.remote upstream

运行上面的set命令时没有任何输出。

查看所有遥控器

通过以下方式查看所有远程名称及其 URL:

git remote -v

-v
的意思是“详细”。

运行和输出示例:

wiki_copy_demo.wiki$ git remote -v
origin  [email protected]:ElectricRCAircraftGuy/wiki_copy_demo.wiki.git (fetch)
origin  [email protected]:ElectricRCAircraftGuy/wiki_copy_demo.wiki.git (push)
upstream    https://github.com/nicolargo/glances.wiki.git (fetch)
upstream    https://github.com/nicolargo/glances.wiki.git (push)

git config
一般细节

您可以通过

git config branch.branch_name.remote
以编程方式读出任何给定分支的本地存储的远程跟踪远程名称。

假设您有一个名为

main
的分支,并且其跟踪的远程设置为
origin
。在这种情况下,您的
.git/config
文件将包含以下内容:

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

运行这个:

git config branch.main.remote

...因此将读出该配置设置并返回

remote
的值,即
origin

运行这个:

git config branch.main.remote upstream

...会将

remote
的值从
origin
(之前的值)更改为
upstream

您可以使用这些模式以编程方式读取或写入任何 git 配置变量,甚至是您自己发明或编写的变量。

示例:此命令:

git config --global blametool.editor subl
将这些行添加到全局
~/.gitconfig
文件的底部:

[blametool]
    editor = subl

您可以通过以下方式读出该变量值

subl
git config blametool.editor

这就是我为我的 git blametool

 脚本
设置指责工具的方式。


0
投票

此信息取决于远程,因此不同的远程(在同一个本地克隆上)可能有不同的默认分支

$ git remote show origin | grep 'HEAD branch'  | cut -d' ' -f5
main

(如有必要,显然可以在任何其他遥控器上重复

origin
upstream
等)


-1
投票

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

git 配置branch.master.pushRemote || git config remote.pushDefault || git configbranch.master.remote

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

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

覆盖

出于某种原因,“man git push”只告诉branch.name.remote(即使它具有三者中最低的优先级)+错误地指出,如果未设置,则推送默认为origin - 事实并非如此,它是只是当你克隆一个仓库时,branch.name.remote被设置为origin,但是如果你删除这个设置,git推送将会失败,即使你仍然有origin远程

© www.soinside.com 2019 - 2024. All rights reserved.