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

发布于 2024-10-14 22:32:11 字数 579 浏览 5 评论 0原文

我有一个远程裸存储库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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

挽心 2024-10-21 22:32:11

您可以做得更简单,保证您的 .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 to hub/master)
git branch branch_name --set-upstream-to hub/master

If you're using v1.7.x or earlier

you must use --set-upstream:
git branch --set-upstream master hub/master

〆凄凉。 2024-10-21 22:32:11

跟踪远程分支

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

编辑您的 .git/config

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

现在 你可以简单地 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

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

Now you can simply git push and git pull.

[source]

时间你老了 2024-10-21 22:32:11

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

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

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:

也只是曾经 2024-10-21 22:32:11

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

“获取”命令

对于名为 branch_name 的分支,请使用以下命令读出它:

git config branch.branch_name.remote

# OR (same thing)
git config --get branch.branch_name.remote

示例:

# main branch
git config branch.main.remote
# or
git config --get branch.main.remote

# master branch
git config branch.master.remote
# or
git config --get 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 文件

如果您想检查 git 整个 config 文件,它们位于例如,在 Linux 上,您应该最常使用的配置文件首先列出:

  1. 特定于存储库的配置文件my_repo/.git/config
    • 通过 git configgit config --local 命令访问。这是默认设置。
  2. 用户特定的配置文件~/.gitconfig
    • 通过 git config --global 命令访问。
  3. 系统范围的配置文件/etc/gitconfig
    • 通过 git config --system 命令访问。

参考:

  1. https://git-scm.com/docs/git-config

如果如果您没有添加 --global--system 配置设置,也没有手动创建这些文件,那么后两个文件将不存在。

要一次读取上述所有 3 个配置文件中的所有设置,请运行以下

# Read all settings from all 3 config files at once
git config --list

# Read all settings from all 3 config files at once, **and** show 
# from which config file each setting is read
git config --list --show-origin

命令: git config --list 的部分命令和输出示例:

$ git config --list
diff.tool=meld
core.editor=code --wait
core.autocrlf=input
core.eol=lf
alias.lg=log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
alias.add-commit=!git add -A && git commit
blametool.editor=subl
blametool.auto-delete-tempfile-when-done=true
user.name=Gabriel Staples

git config --list 的部分命令和输出示例 - -show-origin,显示设置来源的配置文件:

$ git config --list --show-origin 
file:/home/gabriel/.gitconfig    diff.tool=meld
file:/home/gabriel/.gitconfig    core.editor=code --wait
file:/home/gabriel/.gitconfig    core.autocrlf=input
file:/home/gabriel/.gitconfig    core.eol=lf
file:/home/gabriel/.gitconfig    alias.lg=log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
file:/home/gabriel/.gitconfig    alias.add-commit=!git add -A && git commit
file:/home/gabriel/.gitconfig    blametool.editor=subl
file:/home/gabriel/.gitconfig    blametool.auto-delete-tempfile-when-done=true
file:.git/config        user.name=Gabriel Staples

您可以手动创建您想要的任何“配置”设置,即使还没有 git 程序使用它

这对于创建您自己的自定义设置和 git 附加程序非常有用。

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

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

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

运行此:

git config branch.main.remote

...因此将读出该配置设置并返回 remote,这是起源

运行此命令:

git config branch.main.remote upstream

...会将 remote 的值从 origin (其先前的值)更改为 upstream

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

示例:此命令:git config --global Blametool.editor subl 将这些行添加到全局 ~/.gitconfig 文件的底部:

[blametool]
    editor = subl

您可以读出该变量值,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:

git config branch.branch_name.remote

# OR (same thing)
git config --get branch.branch_name.remote

Examples:

# main branch
git config branch.main.remote
# or
git config --get branch.main.remote

# master branch
git config branch.master.remote
# or
git config --get branch.master.remote

Sample output:

origin

"Set" commands

Change the default remote for a branch like this:

# 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

There is no output when running the set commands just above.

See all remotes

See all of your remote names and their URLs with:

git remote -v

The -v means "verbose".

Example run and output:

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 general details

Read your entire git config file

If 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:

  1. Repository-specific config file: my_repo/.git/config
    • Accessed via git config or git config --local commands. This is the default.
  2. User-specific config file: ~/.gitconfig
    • Accessed via git config --global commands.
  3. System-wide config file: /etc/gitconfig
    • Accessed via git config --system commands.

Reference:

  1. https://git-scm.com/docs/git-config

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:

# Read all settings from all 3 config files at once
git config --list

# Read all settings from all 3 config files at once, **and** show 
# from which config file each setting is read
git config --list --show-origin

Example partial command and output of git config --list:

$ git config --list
diff.tool=meld
core.editor=code --wait
core.autocrlf=input
core.eol=lf
alias.lg=log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
alias.add-commit=!git add -A && git commit
blametool.editor=subl
blametool.auto-delete-tempfile-when-done=true
user.name=Gabriel Staples

Example partial command and output of git config --list --show-origin, showing the config file from which the settings come:

$ git config --list --show-origin 
file:/home/gabriel/.gitconfig    diff.tool=meld
file:/home/gabriel/.gitconfig    core.editor=code --wait
file:/home/gabriel/.gitconfig    core.autocrlf=input
file:/home/gabriel/.gitconfig    core.eol=lf
file:/home/gabriel/.gitconfig    alias.lg=log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
file:/home/gabriel/.gitconfig    alias.add-commit=!git add -A && git commit
file:/home/gabriel/.gitconfig    blametool.editor=subl
file:/home/gabriel/.gitconfig    blametool.auto-delete-tempfile-when-done=true
file:.git/config        user.name=Gabriel Staples

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 to origin. In that case, your .git/config file will contain this, among other things:

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

Running this:

git config branch.main.remote

...will therefore read out that configuration setting and return the value of remote, which is origin.

And running this:

git config branch.main.remote upstream

...will change the value of remote from origin (its previous value) to upstream.

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:

[blametool]
    editor = subl

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.

路弥 2024-10-21 22:32:11

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

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

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

The programmatic version of the answer to this question is:

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

This will output just the current branch's default remote name. The --show-current option will not work before Git version 2.22.0.

薄情伤 2024-10-21 22:32:11

获取分支(例如 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文