502

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 = git@github.com: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.

David Smith
  • 38,044
  • 11
  • 44
  • 61
  • What does your .git/config look like? What did you do after you checked out that commit? – Ryann Graham Mar 18 '09 at 16:33
  • Did you do commits on top of 4f82a29? – Pat Notz Mar 18 '09 at 17:01
  • Pat, I did not do any commits on top of it. This is on a server, and we needed to roll back to a stable version in order to hide a bug we had created. This system is not for development purposes, so I simply wanted to roll back, wait while we fixed the bug, and then pull back to the head version. – David Smith Mar 18 '09 at 18:02
  • 2
    Ryan, I've updated to include the .git/config. After the checkout, I didn't do anything. This computer is a server, not for development. – David Smith Mar 18 '09 at 18:03

9 Answers9

750

Under [branch "master"], try adding the following to the repo's Git config file (.git/config):

[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 remote 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).

If you don't want to edit the config file by hand, you can use the command-line tool instead:

$ git config branch.master.remote origin
$ git config branch.master.merge refs/heads/master
k_rollo
  • 5,304
  • 16
  • 63
  • 95
mipadi
  • 398,885
  • 90
  • 523
  • 479
  • 2
    This worked for me as well, I had checked out a project from github. I'm running OS X 10.4 – Sam Barnum May 24 '09 at 17:19
  • 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! – chesterbr Mar 08 '10 at 12:05
  • See answer by Head to avoid having to edit the config file by hand – pjb3 Oct 21 '10 at 20:04
  • 1
    What do you mean by Under [branch "master"] – ianj Dec 17 '10 at 16:57
  • 3
    @ianj: In the Git config file (from the repo root, `.git/config`). – mipadi Dec 17 '10 at 17:10
  • 1
    @ianj: From the command line, you can always do `$ git config branch.master.remote origin ; git config branch.master.merge refs/heads/master` instead. – mipadi Dec 17 '10 at 17:11
  • +1 from me. I've just installed git 1.7.4 and have been working through the excellent book 'Version Control with Git'. It seems that 1.7.4 doesn't seem to set the repository up the same way as the book suggests, something that other users have had problems with judging by the erratta on the book's web site. – GrandMasterFlush Jun 02 '11 at 08:13
  • I had this problem with a new branch and this worked for me, just had to add that branch and lines to my .git/config replacing master with my other branch's name! +1 – Terri Ann Oct 03 '11 at 18:01
  • @mipadi, your answer is absolutely correct, thanks a lot. Just FYI, I use `git remote show origin` to see if it is safe to use `git pull` or `git push`. If there is any problem, I will use `git branch --set-upstream master origin/master` to do the same thing as you said, but only no need to bother with `refs/heads/master`. I think a lot of people don't what does `refs/heads/master` mean. :) – Tonny Xu Dec 01 '11 at 13:34
  • Better to use git branch --set-upstream as referenced in one of the other answers. (I think this is fairly new though) – brendanjerwin Feb 01 '12 at 02:53
  • @ianj: If the file doesn't have a [branch "master"] line in it already, add that line, then add the subsequent lines in the answer. – reor Mar 08 '14 at 06:13
  • added this one as global using --global flag after the config – ssi-anik Nov 21 '16 at 15:35
  • git config --global branch.main.remote origin && git config --global branch.main.merge refs/heads/main – seunggabi Jan 24 '23 at 08:54
142

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
  • 4,691
  • 3
  • 30
  • 18
  • 12
    +1 for knowing the magic word "refs/heads/master". I had no trouble figuring out how to set the variable, but had absolutely no clue what to set it *to*, and the man pages weren't much help. I eventually found the right place in the docs after I found this answer. For the curious: the magic word refers to a file path in `.git` in which git appears to keep the hash code of `master`s current commit. – mokus Apr 27 '11 at 16:47
88
git branch --set-upstream master origin/master

This will add the following info to your config file:

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

If you have branch.autosetuprebase = always then it will also add:

    rebase = true
cmcginty
  • 113,384
  • 42
  • 163
  • 163
  • 1
    I find this the easiest way to make git behave like asked, especially if there are more branches, not just remote (even if you have to do this for every branch, it's one time per branch) – s3v3n Mar 21 '12 at 16:44
  • 2
    I just tried this, and I get the error `fatal: Not a valid object name: 'origin/master'.` even though `origin` is a valid remote, and `master` exists, as per usual, in both repos. – Ken Williams Apr 06 '12 at 14:00
  • 2
    Ken, you need to do "git fetch origin" first to get the remote branch names. – Eric Lee Apr 23 '12 at 18:48
  • 17
    Newer git wants you to use `git branch --set-upstream-to=origin/master master`. – orbeckst Sep 03 '13 at 18:17
54

I find it hard to remember the exact git config or git branch arguments as in mipadi's and Casey's answers, so I use these 2 commands to add the upstream reference:

git pull origin master
git push -u origin master

This will add the same info to your .git/config, but I find it easier to remember.

24

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
  • 16,255
  • 3
  • 33
  • 25
  • 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. – David Smith Mar 18 '09 at 19:19
  • I just misread your situation. I'm glad you got the answer you needed. – Paul Mar 19 '09 at 05:29
17

There is also a way of configuring Git so, it always pulls and pushes the equivalent remote branch to the branch currently checked out to the working copy. It's called a tracking branch which git ready recommends setting by default.

For the next repository above the present working directory:

git config branch.autosetupmerge true

For all Git repositories, that are not configured otherwise:

git config --global branch.autosetupmerge true

Kind of magic, IMHO but this might help in cases where the specific branch is always the current branch.

When you have branch.autosetupmerge set to true and checkout a branch for the first time, Git will tell you about tracking the corresponding remote branch:

(master)$ git checkout gh-pages
Branch gh-pages set up to track remote branch gh-pages from origin.
Switched to a new branch 'gh-pages'

Git will then push to that corresponding branch automatically:

(gh-pages)$ git push
Counting objects: 8, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 1003 bytes, done.
Total 6 (delta 2), reused 0 (delta 0)
To git@github.com:bigben87/webbit.git
   1bf578c..268fb60  gh-pages -> gh-pages
Bengt
  • 14,011
  • 7
  • 48
  • 66
  • Thank you! This is exactly what I was looking for. I wonder why doesn't git automatically init with this option, as when you check out a new branch you want to track a remote with the same name 99% of the time – AlexanderD Apr 06 '22 at 10:11
9

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

$ git pull origin master
4

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
Ryann Graham
  • 8,079
  • 2
  • 29
  • 32
0

Just wanted to add some info that, we can check this info whether git pull automatically refers to any branch or not.

If you run the command, git remote show origin, (assuming origin as the short name for remote), git shows this info, whether any default reference exists for git pull or not.

Below is a sample output.(taken from git documentation).

$ git remote show origin
* remote origin
  Fetch URL: https://github.com/schacon/ticgit
  Push  URL: https://github.com/schacon/ticgit
  HEAD branch: master
  Remote branches:
    master                               tracked
    dev-branch                           tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

Please note the part where it shows, Local branch configured for git pull.

In this case, git pull will refer to git pull origin master

Initially, if you have cloned the repository, using git clone, these things are automatically taken care of. But if you have added a remote manually using git remote add, these are missing from the git config. If that is the case, then the part where it shows "Local branch configured for 'git pull':", would be missing from the output of git remote show origin.

The next steps to follow if no configuration exists for git pull, have already been explained by other answers.

dr_dev
  • 492
  • 3
  • 17