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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4847101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:58:55  来源:igfitidea点击:

Git: which is the default configured remote for branch?

gitgit-pushremote-branch

提问by leonbloy

I have a remote bare repository hub. I work only in the masterbranch. 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?

我有一个远程裸仓库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.

回答by urschrei

You can do it more simply, guaranteeing that your .gitconfigis left in a meaningful state:

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

Using Git version v1.8.0 and above

使用 Git 版本 v1.8.0 及以上

git push -u hub masterwhen pushing, or:
git branch -u hub/master

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

OR

或者

(This will set the remote for the currently checked-out branchto hub/master)
git branch --set-upstream-to hub/master

(这会将当前签出分支的遥控器设置为hub/master
git branch --set-upstream-to hub/master

OR

或者

(This will set the remote for the branch named branch_nameto hub/master)
git branch branch_name --set-upstream-to hub/master

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

If you're using v1.7.xor earlier

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

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

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

回答by scragz

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:

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

Edit your .git/config

编辑您的 .git/config

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

Now you can simply git push and git pull.

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

[source]

[来源]

回答by leonbloy

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 -vvshows that info for all branches. (formatted in blue in most terminals)

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

cat .git/configshows this also.

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

For reference:

以供参考:

回答by darkdiatel

the command to get the effective push remote for the branch, e.g., master, is:

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

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

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

Here's why (from the "man git config" output):

这就是原因(来自“man git config”输出):

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[...]

branch.name.remote[...] 告诉 git fetch 和 git push 从哪个远程获取/推送到 [...] [for push] 可能会被remote.pushDefault(对于所有分支)[and]覆盖当前分支 [..] 进一步被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

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