为什么 Git 不默认为“origin master”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5770016/
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 10:44:46  来源:igfitidea点击:

Why does Git not default to "origin master"?

git

提问by nonopolarity

I tried many times doing a

我试过很多次做一个

git pull

and it says the branch is not specified. So the answer in How do you get git to always pull from a specific branch?seems to work.

它说没有指定分支。那么如何让 git 始终从特定分支中提取?似乎工作。

But I wonder, why doesn't Git default to masterbranch? If nothing is specified, doesn't it make sense to mean the masterbranch? Is Git trying to live up to its name - "a git" = an unpleasant person, so that when you ask what time it is, it always tells you, "since you didn't say if you want to know the local time or Greenwich Mean Time, abort."

但我想知道,为什么 Git 不默认master分支?如果未指定任何内容,表示master分支是否有意义?Git 是否正努力实现它的名字——“a git”= 一个不愉快的人,所以当你问现在几点时,它总是告诉你,“因为你没有说你是否想知道当地时间或格林威治标准时间,中止。”

回答by Mark Longair

git tries to use sensible defaults for git pullbased on the branch that you're currently on. If you get the error you refer to from git pullwhile you're on master, that means you haven't configured an upstream branch for master. In many situations this will be configured already - for example, when you clone from a repository, git clonewill set up the originremote to point to that repository and set up masterwith origin/masteras upstream, so git pullwill Just Work?.

git 尝试git pull根据您当前所在的分支使用合理的默认值。如果你让你从参考错误git pull,而你的master,这意味着你没有配置为上游分支master。在许多情况下,这将设置好的-例如,当你从一个仓库克隆,git clone将设置origin遥控器指向该存储库,并建立masterorigin/master上游,所以git pull将只工作?

However, if you want to do that configuration by hand, you can do so with:

但是,如果您想手动进行该配置,您可以使用:

 git branch --set-upstream master origin/master

... although as of git 1.8.0 you should use --set-upstream-to, since the former usage is not deprecated due to be confusingly error-prone. So, for git 1.8.0 and later you should do:

...尽管从 git 1.8.0 开始,您应该使用--set-upstream-to,因为以前的用法不会因为容易混淆而被弃用。因此,对于 git 1.8.0 及更高版本,您应该执行以下操作:

 git branch --set-upstream-to origin/master

Or you could likewise set up the appropriate configuration when pushing with:

或者你也可以在推送时设置适当的配置:

 git push -u origin master

... and git pullwill do what you want.

......并且git pull会做你想做的。

回答by zahanm

As mentioned above, git clonesets all the defaults appropriately. I've found pushing with the -uoption to be the easiest to set things up with an existent repo.

如上所述,git clone适当地设置所有默认值。我发现使用现有-u的存储库最容易设置选项。

git push -u origin master

git push -u origin master

You can check what has been remotely configured using

您可以使用以下命令检查远程配置的内容

git remote -v

git remote -v

See git help remotefor more information on this.

有关这git help remote方面的更多信息,请参阅。

回答by ralphtheninja

Why would you want to default a pull from the master branch? What if I had a repository with no branch named master? After all, it's just a branch named "master".

为什么要默认从 master 分支拉取?如果我有一个没有名为 master 的分支的存储库怎么办?毕竟,它只是一个名为“master”的分支。

My answer to your question is that git doesn't want to get in your way and decide for you what the work flow should be. Normally this isn't a problem since you normally have the remote branch setup already, so git knows what to do when you use "git pull" with no arguments.

我对您的问题的回答是 git 不想妨碍您并为您决定工作流程应该是什么。通常这不是问题,因为您通常已经设置了远程分支,所以当您使用不带参数的“git pull”时,git 知道该怎么做。