How to set to pull the same branch by default?

5,356

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:
5,356

Related videos on Youtube

Xiè Jìléi
Author by

Xiè Jìléi

+--[ RSA 1024]----+ | | | | | . . o | | . o o o o | | S . o + .| | . . + + o | | . . . . . o + | | = o . o . | | =.. ooE| +-----------------+ 1024 aa:0c:4f:fb:15:5c:7e:4c:e7:f1:ca:61:8a:fd:0f:fe X.J. Lenik (RSA)

Updated on September 18, 2022

Comments

  • Xiè Jìléi
    Xiè Jìléi almost 2 years

    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?

  • Dan Rosenstark
    Dan Rosenstark about 13 years
    +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 ;)