Git:哪个是分支的默认配置远程?
我有一个远程裸存储库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.
I have a remote bare repository hub
. I work only in the master
branch.
The last sentence of this error message below makes me wonder: How do I find out which is the "default configured remote for your current branch"? And how do I set it?
[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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以做得更简单,保证您的
.gitconfig
处于有意义的状态:使用 Git 版本 v1.8.0 及更高版本
在推送时
git push -u hub master
,或:git Branch -u hub/master
或者
(这会将当前签出的分支的远程设置设置为
hub/master
< /强>)gitbranch --set-upstream-to hub/master
或者
(这会将名为
branch_name
的分支的远程设置为hub/master
)
gitbranch_name --set-upstream-to hub/master
如果您使用的是
v1.7.x
或更早版本,则必须使用
--set-upstream :
git分支--set-upstream master hub/master
You can do it more simply, guaranteeing that your
.gitconfig
is left in a meaningful state:Using Git version v1.8.0 and above
git push -u hub master
when pushing, or:git branch -u hub/master
OR
(This will set the remote for the currently checked-out branch to
hub/master
)git branch --set-upstream-to hub/master
OR
(This will set the remote for the branch named
branch_name
tohub/master
)git branch branch_name --set-upstream-to hub/master
If you're using
v1.7.x
or earlieryou must use
--set-upstream
:git branch --set-upstream master hub/master
跟踪远程分支
您可以使用 git-branch 的 track 选项指定用于推送和拉取的默认远程存储库。通常,您可以通过在创建本地 master 分支时指定 --track 选项来完成此操作,但由于它已经存在,我们只需手动更新配置,如下所示:
编辑您的
.git/config
现在 你可以简单地 git push 和 git pull 。
[来源]
Track the remote branch
You can specify the default remote repository for pushing and pulling using git-branch’s track option. You’d normally do this by specifying the --track option when creating your local master branch, but as it already exists we’ll just update the config manually like so:
Edit your
.git/config
Now you can simply git push and git pull.
[source]
为了完整起见:前面的答案告诉了如何设置上游分支,但没有告诉如何查看它。
有几种方法可以做到这一点:
gitbranch -vv 显示所有分支的信息。 (在大多数终端中格式为蓝色)
cat .git/config
也显示了这一点。供参考:
For the sake of completeness: the previous answers tell how to set the upstream branch, but not how to see it.
There are a few ways to do this:
git branch -vv
shows that info for all branches. (formatted in blue in most terminals)cat .git/config
shows this also.For reference:
Git:哪个是分支的默认配置远程?
“获取”命令
对于名为
branch_name
的分支,请使用以下命令读出它:示例:
示例输出:
“设置”命令
更改分支的默认远程,如下所示
:运行上面的 set 命令时没有输出。
查看所有遥控器
查看所有遥控器名称及其 URL:
-v
表示“详细”。示例运行和输出:
git config
一般详细信息读取整个 git
config
文件如果您想检查 git 整个
config
文件,它们位于例如,在 Linux 上,您应该最常使用的配置文件首先列出:my_repo/.git/config
git config
或git config --local
命令访问。这是默认设置。~/.gitconfig
git config --global
命令访问。/etc/gitconfig
git config --system
命令访问。参考:
如果如果您没有添加
--global
或--system
配置设置,也没有手动创建这些文件,那么后两个文件将不存在。要一次读取上述所有 3 个配置文件中的所有设置,请运行以下
命令: git config --list 的部分命令和输出示例:
git config --list 的部分命令和输出示例 - -show-origin,显示设置来源的配置文件:
您可以手动创建您想要的任何“配置”设置,即使还没有 git 程序使用它
这对于创建您自己的自定义设置和 git 附加程序非常有用。
您可以通过 git configbranch.branch_name.remote 以编程方式读出任何给定分支的本地存储的远程跟踪远程名称。 。
假设您有一个名为
main
的分支,并且它跟踪的远程分支设置为origin
。在这种情况下,您的.git/config
文件将包含以下内容:运行此:
...因此将读出该配置设置并返回
remote
,这是起源
。运行此命令:
...会将
remote
的值从origin
(其先前的值)更改为upstream
。您可以使用这些模式以编程方式读取或写入任何 git 配置变量,甚至是您自己发明或编写的变量。
示例:此命令:
git config --global Blametool.editor subl
将这些行添加到全局~/.gitconfig
文件的底部:您可以读出该变量值,
subl
,带有:git configblametool.editor
。这就是我为我的
gitblametool
脚本设置blametool的方法。Git: which is the default configured remote for a branch?
"Get" commands
For a branch named
branch_name
, read it out with this:Examples:
Sample output:
"Set" commands
Change the default remote for a branch like this:
There is no output when running the set commands just above.
See all remotes
See all of your remote names and their URLs with:
The
-v
means "verbose".Example run and output:
git config
general detailsRead your entire git
config
fileIf you want to inspect your git entire
config
file, they are located here, for instance, on Linux, with the one you should be using most listed first:my_repo/.git/config
git config
orgit config --local
commands. This is the default.~/.gitconfig
git config --global
commands./etc/gitconfig
git config --system
commands.Reference:
If you haven't added
--global
or--system
config settings, nor manually created those files, then the latter two files will not exist.To read all settings from all 3 config files above at once, run this:
Example partial command and output of
git config --list
:Example partial command and output of
git config --list --show-origin
, showing the config file from which the settings come:You can manually create any "config" setting you want, even if no git program uses it yet
This is useful for creating your own custom settings and git add-on programs.
You can programmatically read out any given branch's locally-stored remote-tracking remote name via
git config branch.branch_name.remote
.Assume that you have a branch named
main
and its remote it tracks is set toorigin
. In that case, your.git/config
file will contain this, among other things:Running this:
...will therefore read out that configuration setting and return the value of
remote
, which isorigin
.And running this:
...will change the value of
remote
fromorigin
(its previous value) toupstream
.You can use these patterns to programmatically read or write any git config variable, even ones you invent or make up yourself.
Example: this command:
git config --global blametool.editor subl
adds these lines to the bottom of your global~/.gitconfig
file:And you can read out that variable value,
subl
, with:git config blametool.editor
.That's how I set a blametool for my
git blametool
script.这个问题的答案的编程版本是:
这将仅输出当前分支的默认远程名称。
--show-current
选项在 Git 版本 2.22.0 之前不起作用。The programmatic version of the answer to this question is:
This will output just the current branch's default remote name. The
--show-current
option will not work before Git version 2.22.0.获取分支(例如 master)的有效推送远程的命令是:
git configbranch.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(即使它有三者中优先级最低的)+错误地指出,如果未设置,则推送默认为原点 - 事实并非如此,只是当您克隆存储库时,branch.name.remote 设置为原点,但如果您删除此设置设置,git推送将会失败,即使你仍然有源远程
the command to get the effective push remote for the branch, e.g., master, is:
git config branch.master.pushRemote || git config remote.pushDefault || git config branch.master.remote
Here's why (from the "man git config" output):
branch.name.remote [...] tells git fetch and git push which remote to fetch from/push to [...] [for push] may be overridden with remote.pushDefault (for all branches) [and] for the current branch [..] further overridden by branch.name.pushRemote [...]
For some reason, "man git push" only tells about branch.name.remote (even though it has the least precedence of the three) + erroneously states that if it is not set, push defaults to origin - it does not, it's just that when you clone a repo, branch.name.remote is set to origin, but if you remove this setting, git push will fail, even though you still have the origin remote