Why do I need to do `--set-upstream` all the time?

2018-12-31 15:21发布

I create a new branch in Git:

git branch my_branch

Push it:

git push origin my_branch

Now say someone made some changes on the server and I want to pull from origin/my_branch. I do:

git pull

But I get:

You asked me to pull without telling me which branch you
want to merge with, and 'branch.my_branch.merge' in
your configuration file does not tell me, either. Please
specify which branch you want to use on the command line and
try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) for details.

If you often merge with the same branch, you may want to
use something like the following in your configuration file:

    [branch "my_branch"]
    remote = <nickname>
    merge = <remote-ref>

    [remote "<nickname>"]
    url = <url>
    fetch = <refspec>

See git-config(1) for details.

I learned that I can make it work with:

git branch --set-upstream my_branch origin/my_branch

But why do I need to do this for every branch I create? Isn't it obvious that if I push my_branch into origin/my_branch, then I would want to pull origin/my_branch into my_branch? How can I make this the default behavior?

18条回答
还给你的自由
2楼-- · 2018-12-31 15:37

By the way, the shortcut to pushing the current branch to a remote with the same name:

$ git push -u origin HEAD
查看更多
姐姐魅力值爆表
3楼-- · 2018-12-31 15:38

I use this Git alias instead of copy/pasting the suggestion from Git every time: https://gist.github.com/ekilah/88a880c84a50b73bd306

Source copied below (add this to your ~/.gitconfig file):

[alias]
  pushup = "!gitbranchname() { git symbolic-ref --short HEAD; }; gitpushupstream() { git push --set-upstream origin `gitbranchname`; }; gitpushupstream"
查看更多
闭嘴吧你
4楼-- · 2018-12-31 15:39

You can also explicitly tell git pull what remote branch to pull (as it mentions in the error message):

git pull <remote-name> <remote-branch>

Be careful with this, however: if you are on a different branch and do an explicit pull, the refspec you pull will be merged into the branch you're on!

查看更多
君临天下
5楼-- · 2018-12-31 15:40

You can use:

git config --global branch.autosetupmerge always

which will link the upstream branch each time you create or checkout a new branch.

See https://felipec.wordpress.com/2013/09/01/advanced-git-concepts-the-upstream-tracking-branch/

This also works with autosetuprebase, if you follow a more rebase focused workflow, but don't use this unless you know what you're doing, as it will default your pull behavior to rebase, which can cause odd results.

查看更多
只靠听说
6楼-- · 2018-12-31 15:40
git branch --set-upstream-to=origin/master<branch_name>
查看更多
萌妹纸的霸气范
7楼-- · 2018-12-31 15:40

For what it is worth, if you are trying to track a branch that already exists on the remote (eg. origin/somebranch) but haven't checked it out locally yet, you can do:

$ git checkout --track origin/somebranch

Note: '-t' is the shortened version of '--track' option.

This sets up the same association right off the bat.

查看更多
登录 后发表回答