views:

13272

answers:

5

I'm not a git master, but I have been working with it for some time now, with several different projects. In each project, I always git clone [repository] and from that point, can always git pull, so long as I don't have outstanding changes, of course.

Recently, I had to revert to a previous branch, and did so with git checkout 4f82a29. When I was again ready to pull, I found that I had to set my branch back to master. Now, I can not pull using a straight git pull but instead, have to specify git pull origin master, which is annoying, and indicates to me that I don't fully understand what is going on.

What has changed which does not allow me to do a straight git pull without specifying origin master, and how to I change it back?

UPDATE:

-bash-3.1$ cat config
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[branch "master"]
[remote "origin"]
    url = [email protected]:user/project.git
    fetch = refs/heads/*:refs/remotes/origin/*

UPDATE 2: To be clear, I understand that my original method may have been incorrect, but I need to fix this repo so that I can simply use git pull again. Currently, git pull results in:

-bash-3.1$ git pull
You asked me to pull without telling me which branch you
want to merge with, and 'branch.master.merge' in
your configuration file does not tell me either.  Please
name which branch you want to merge on the command line and
try again (e.g. 'git pull  ').
See git-pull(1) for details on the refspec.

If you often merge with the same branch, you may want to
configure the following variables in your configuration
file:

    branch.master.remote = 
    branch.master.merge = 
    remote..url = 
    remote..fetch = 

See git-config(1) for details.

I can tell git pull which branch to merge, and it works correctly, but git pull does not work as it did originally before my git checkout.

+4  A: 

Your immediate question of how to make it pull master, you need to do what it says. Specify the refspec to pull from in your branch config.

[branch "master"]
    merge = refs/heads/master
Ryan Graham
Shouldn't that be "refs/heads/master"? According to git-pull(1), this is the name of the branch at the remote site that is merged by default.
Adam Monsen
Yes, you're correct. The repo I took my example from is a special case. Corrected.
Ryan Graham
+5  A: 

Git pull combines two actions -- fetching new commits from the remote repository in the tracked branches and then merging them into your current branch.

When you checked out a particular commit, you don't have a current branch, you only have HEAD pointing to the last commit you made. So git pull doesn't have all its parameters specified. That's why it didn't work.

Based on your updated info, what you're trying to do is revert your remote repo. If you know the commit that introduced the bug, the easiest way to handle this is with git revert which records a new commit which undoes the specified buggy commit:

$ git checkout master
$ git reflog            #to find the SHA1 of buggy commit, say  b12345
$ git revert b12345
$ git pull
$ git push

Since it's your server that you are wanting to change, I will assume that you don't need to rewrite history to hide the buggy commit.

If the bug was introduced in a merge commit, then this procedure will not work. See How-to-revert-a-faulty-merge.

Paul
You're giving me some great education here, which I appreciate, but I might not be describing my situation very well, so this isn't an exact match for my workflow. I'll probably post another question to address that. Thanks though, Paul! +1 to you, Sir.
BigDave
I just misread your situation. I'm glad you got the answer you needed.
Paul
+65  A: 

Under [branch "master"], try adding:

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

This tells Git 2 things:

  1. When you're on the master branch, the default remote is origin.
  2. When using git pull on the master branch, with no remote and branch specified, use the default remote (origin) and merge in the changes from the master branch.

I'm not sure why this setup would've been removed from your configuration, though. You may have to follow the suggestions that other people have posted, too, but this may work (or help at least).

mipadi
That did the trick. Thank you!
BigDave
This worked for me as well, I had checked out a project from github. I'm running OS X 10.4
Sam Barnum
+1 - Thanks, worked for me too!
Topher Fangio
Thank you *very* much - this happened to me on a single developer project with one "server" repository and two computers (which I used to push/pull frequently with no issues before the glitch), don't know why, but the fix worked fine!
chester
Thanks for the hint, problem solved here as well!
NiKo
+1, worked perfectly for me as well, thanks! still haven't worked out exactly how my config got into this state in the first place, though.
Paul Sanwald
See answer by Head to avoid having to edit the config file by hand
pjb3
+2  A: 

Not wanting to edit my git config file I followed the info in @mipadi's post and used:

$ git pull origin master
+31  A: 

If you prefer, you can set these options via the commmand line (instead of editing the config file) like so:

  $ git config branch.master.remote origin
  $ git config branch.master.merge refs/heads/master

Or, if you're like me, and want this to be the default across all of your projects, including those you might work on in the future, then add it as a global config setting:

  $ git config --global branch.master.remote origin
  $ git config --global branch.master.merge refs/heads/master
Head
Thanks, used the --global option.
John Ballinger