38

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.

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?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • 1
    You made no indication as to how you created that repo in the first place. You obviously didn't do a plain `git clone` of an upstream that it could name `origin` and which has a `master` branch or it would've set it up. If no `master` or no `origin` exists, how can it push to it? – Dustin Apr 24 '11 at 15:29
  • both `origin` and `master` exist. That's why `git push origin master` works, but `git push` doesn't – nonopolarity Apr 27 '11 at 07:09
  • 動靜能量, you only responded to the end of my comment. How did you create that repository? If you did a clone, it would've done what you expect. You did something unusual and you got unusual results. – Dustin Apr 27 '11 at 16:17
  • @Dustin I didn't see your comment earlier... let's just even look at `git push`, if we have a repo that was just created using `git init`, and let's say you add a remote `origin` already. So `git push` could default to `origin master` – nonopolarity Apr 18 '17 at 18:18
  • https://www.cnet.com/news/microsofts-github-is-removing-coding-terms-like-master-and-slave/ – Rainb Jun 21 '20 at 20:24

3 Answers3

85

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.

Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • 7
    from my 20 years of programming experience, git is harder to use that C or C++, Java, or even machine code! – nonopolarity Apr 24 '11 at 11:21
  • 4
    @動靜能量: that really makes me wonder about the complexity of the coding you've done... Granted: sometimes the options and features are well hidden, but nothing a vim .git/config with `git config --help` couldn't resolve – sehe Apr 24 '11 at 13:19
  • 1
    @動靜能量: Really, it's nowhere near as bad as that :) In fact, I think the fundamental concepts of git are essentially very simple and elegant, it's just that the command line interface takes some learning. I'd recommend the book ["Pro Git"](http://progit.org/) if you haven't already looked at it, or one of the bottom-up tutorials like [git for computer scientists](http://eagain.net/articles/git-for-computer-scientists/). You can *almost* learn everything you need to know about git from the [glossary](http://www.kernel.org/pub/software/scm/git/docs/gitglossary.html), I think :) – Mark Longair Apr 24 '11 at 23:02
  • 2
    @sehe yeah I know somebody is going to say something like this... if you say, "that travel agent is really bad! in my 20 years of traveling, I have never seen an agent that actually took the money and ran", and there bound to be somebody that said, "you haven't seen nothing yet", or "that makes me wonder how much traveling you have done in your 20 years" – nonopolarity Apr 27 '11 at 07:15
  • 3
    @動靜能量: true, and I don't really doubt your experience; it was more or less my way of telling you there is no need to convince anyone here of the ardour of learning things. We are here to help you learn it, and complaining students just frankly are less fun :). (_If you talk to my colleagues, you'll know that the amount of 'WTFs' I utter through the day is a good measure of how much I'm learning myself that day. I just don't include that on SO_) -- Cheers – sehe Apr 27 '11 at 08:45
  • 1
    `--set-upstream` is deprecated, use `git branch --set-upstream-to origin/master` – Bengt Mar 09 '13 at 19:49
  • @bngtlrs: thanks for pointing that out - I'd updated my other answer that deals with this, but not this one. I've done that now. – Mark Longair Mar 10 '13 at 21:15
7

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.

zahanm
  • 993
  • 8
  • 11
0

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.

rtn
  • 127,556
  • 20
  • 111
  • 121