Why does Git not default to "origin master"?
I tried many times doing a
git pull
and it says the branch is not specified开发者_如何学Go. So the answer in How do you get git to always pull from a specific branch? seems to work.
But I wonder, why doesn't Git default to master
branch? If nothing is specified, doesn't it make sense to mean the master
branch?
git tries to use sensible defaults for git pull
based on the branch that you're currently on. If you get the error you refer to from git pull
while 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 clone
will set up the origin
remote to point to that repository and set up master
with origin/master
as upstream, so git pull
will Just Work™.
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 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 pull
will do what you want.
As mentioned above, git clone
sets all the defaults appropriately.
I've found pushing with the -u
option to be the easiest to set things up with an existent repo.
git push -u origin master
You can check what has been remotely configured using
git remote -v
See git help remote
for more information on this.
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".
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.
精彩评论