David Smith David Smith - 1 year ago 65
Git Question

How do you get git to always pull from a specific branch?

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?


-bash-3.1$ cat config
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

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

Answer Source

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