200

I have a remote bare repository hub. I work only in the master branch. The last sentence of this error message below makes me wonder: How do I find out which is the "default configured remote for your current branch"? And how do I set it?

[myserver]~/progs $ git remote -v
hub     ~/sitehub/progs.git/ (fetch)
hub     ~/sitehub/progs.git/ (push)

[myserver]~/progs $ git branch -r
  hub/master

[myserver]~/progs $ cat .git/HEAD
ref: refs/heads/master

[myserver]~/progs $ git pull hub
You asked to pull from the remote 'hub', but did not specify
a branch. Because this is not the default configured remote
for your current branch, you must specify a branch on the command line.
leonbloy
  • 65,169
  • 19
  • 130
  • 176

4 Answers4

264

You can do it more simply, guaranteeing that your .gitconfig is left in a meaningful state:

Using Git version v1.8.0 and above

git push -u hub master when pushing, or:
git branch -u hub/master

OR

(This will set the remote for the currently checked-out branch to hub/master)
git branch --set-upstream-to hub/master

OR

(This will set the remote for the branch named branch_name to hub/master)
git branch branch_name --set-upstream-to hub/master

If you're using v1.7.x or earlier

you must use --set-upstream:
git branch --set-upstream master hub/master

urschrei
  • 21,261
  • 12
  • 36
  • 75
  • 3
    For anyone wondering: the second command can be used for existing branches – Eric Hu Aug 22 '11 at 20:54
  • 1
    @eric-hu as detailed in my answer here: http://stackoverflow.com/questions/4878249/how-do-i-change-the-remote-a-git-branch-is-tracking/4879224#4879224 – urschrei Aug 22 '11 at 21:36
  • 1
    The `set-upstream[-to]` command changes the **currently** configured remote. The original poster asked about the **default** configured remote. Surely that's not quite the same concept? – Steve Pitchers Nov 21 '13 at 18:30
  • @StevePitchers Does git distinguish between them? Feel free to suggest an improvement / clarification, or, indeed your own answer. – urschrei Nov 22 '13 at 17:10
  • 1
    Each branch has a **currently** configured remote, specifying which branch on that remote corresponds to the local branch. The **default** configured remote determines which branch is pushed or pulled if you don't specify one explicitly. This answer only sets the **current** one. The accepted answer (editing by hand) also allows you to set the **default** one. Does anyone know a command that avoids having to edit by hand? – Steve Pitchers Nov 25 '13 at 09:53
  • 1
    `--set-upstream-to` made exactly the same changes in `.git/config` as @scragz suggested in [his answer](http://stackoverflow.com/a/4847136/852866). – strah Mar 14 '14 at 16:36
237

Track the remote branch

You can specify the default remote repository for pushing and pulling using git-branch’s track option. You’d normally do this by specifying the --track option when creating your local master branch, but as it already exists we’ll just update the config manually like so:

Edit your .git/config

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

Now you can simply git push and git pull.

[source]

Mohsen
  • 58,878
  • 30
  • 149
  • 175
scragz
  • 6,572
  • 2
  • 21
  • 23
27

For the sake of completeness: the previous answers tell how to set the upstream branch, but not how to see it.

There are a few ways to do this:

git branch -vv shows that info for all branches. (formatted in blue in most terminals)

cat .git/config shows this also.

For reference:

Community
  • 1
  • 1
leonbloy
  • 65,169
  • 19
  • 130
  • 176
-2

the command to get the effective push remote for the branch, e.g., master, is:

git config branch.master.pushRemote || git config remote.pushDefault || git config branch.master.remote

Here's why (from the "man git config" output):

branch.name.remote [...] tells git fetch and git push which remote to fetch from/push to [...] [for push] may be overridden with remote.pushDefault (for all branches) [and] for the current branch [..] further overridden by branch.name.pushRemote [...]

For some reason, "man git push" only tells about branch.name.remote (even though it has the least precedence of the three) + erroneously states that if it is not set, push defaults to origin - it does not, it's just that when you clone a repo, branch.name.remote is set to origin, but if you remove this setting, git push will fail, even though you still have the origin remote