Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

When I pull from the new added remotes, I have to specify the branch name explicitly:

$ git pull remote1
... 
Error: you didn't specify a branch name.

$ git pull remote1 master

Though I'm working on the master branch.

It maybe useful to pull from a different branch from different remotes, but it never happened to me. In our team, we never pull a branch of different name to the current checked out branch.

So, can I configure git to always pull from the branch with the same name?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

If you do not want to reset the “upstream configuration” of your local branch, you could use this:

git pull remote1 "$(git rev-parse --symbolic-full-name HEAD)"

It will probably do unexpected things if your local HEAD is detached.

You could package it up as a Git alias to make it easier to type (though you would still need to distribute the alias to all your users):

git config --global alias.pullcb \
 '!sh -c '\''git pull "$1" "$(git rev-parse --symbolic-full-name HEAD)"'\'' -'

Use the alias like this:

git pullcb remote1

I would guess that this type of branch relationship (need to easily pull from the same named branch on any of several remotes) is not easily configurable because it is rarely useful when most work is done in topic branches.


On the other hand, if the “new remote” is going to be the new primary upstream source for your local branch, then you should consider updating the “upstream configuration” of your local branch so you can simply use git pull (without specifying a remote name).

You can check the upstream configuration for a branch in Git 1.7.0 and later1 with

git rev-parse --symbolic-full-name my-branch@{upstream}

You can update the upstream configuration in Git 1.7.0 and later2 with

git branch --set-upstream my-branch some-remote/their-branch

Once your branch has the appropriate upstream configuration, you can simply use git pull to pull from the current branch’s “upstream branch”.


Ultimately, the upstream configuration of a branch is controlled by the branch.<name>.remote and branch.<name>.merge configuration variables (see git-config(1)). The --track and --no-track options of git branch and git checkout can be used to control whether these variables are set when a branch is created. The branch.autoSetupMerge configuration variable controls the default behavior (when neither --track, nor --no-track are given); it defaults to creating the upstream configuration for branches created from remote-tracking branches. Thus, the following commands create the new local branch “my-branch” and configure it to track the branch “their-branch” from the remote “some-remote”:

git branch my-branch some-remote/their-branch
git checkout -b my-branch some-remote/their-branch

1 For Git versions prior to 1.7.0, you can check the upstream configuration for “my-branch” like this:

git config branch.my-branch remote && git config branch.my-branch.merge

See git-config(1) for descriptions of the values.

You can also manually inspect the .git/config file.

2 For Git versions prior to 1.7.0, you can change the upstream configuration for “my-branch” like this:

git config branch.my-branch.remote some-remote &&
git config branch.my-branch.merge refs/heads/their-branch

See git-config(1) for descriptions of the values.

You can also manually edit the .git/config file (git config -e in Git 1.6.3 and later).

share|improve this answer
    
+1 for git config -e, as I didn't know that... I hope to get some time to check out the rest of the answer ;) –  Yar Apr 23 '11 at 6:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.