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

后端 未结 21 2022
南方客
南方客 2020-11-22 09:15

I create a new branch in Git:

git branch my_branch

Push it:

git push origin my_branch

Now say someone mad

相关标签:
21条回答
  • 2020-11-22 09:38

    You can set upstream simpler in two ways. First when you create the branch:

    git branch -u origin/my-branch
    

    or after you have created a branch, you can use this command.

    git push -u origin my-branch
    

    You can also branch, check out and set upstream in a single command:

    git checkout -b my-branch -t origin/my-branch
    

    My personally preference is to do this in a two-step command:

    git checkout -b my-branch
    git push -u origin my-branch
    
    0 讨论(0)
  • 2020-11-22 09:39

    A shortcut, which doesn't depend on remembering the syntax for git branch --set-upstream 1 is to do:

    git push -u origin my_branch
    

    ... the first time that you push that branch. Or, to push to the current branch to a branch of the same name (handy for an alias):

    git push -u origin HEAD
    

    You only need to use -u once, and that sets up the association between your branch and the one at origin in the same way as git branch --set-upstream does.

    Personally, I think it's a good thing to have to set up that association between your branch and one on the remote explicitly. It's just a shame that the rules are different for git push and git pull.


    1 It may sound silly, but I very frequently forget to specify the current branch, assuming that's the default - it's not, and the results are most confusing :)

    Update 2012-10-11: Apparently I'm not the only person who found it easy to get wrong! Thanks to VonC for pointing out that git 1.8.0 introduces the more obvious git branch --set-upstream-to, which can be used as follows, if you're on the branch my_branch:

    git branch --set-upstream-to origin/my_branch
    

    ... or with the short option:

    git branch -u origin/my_branch
    

    This change, and its reasoning, is described in the release notes for git 1.8.0, release candidate 1:

    It was tempting to say git branch --set-upstream origin/master, but that tells Git to arrange the local branch origin/master to integrate with the currently checked out branch, which is highly unlikely what the user meant. The option is deprecated; use the new --set-upstream-to (with a short-and-sweet -u) option instead.

    0 讨论(0)
  • 2020-11-22 09:42

    For those looking for an alias that works with git pull, this is what I use:

    alias up="git branch | awk '/^\\* / { print \$2 }' | xargs -I {} git branch --set-upstream-to=origin/{} {}"
    

    Now whenever you get:

    $ git pull
    There is no tracking information for the current branch.
    ...
    

    Just run:

    $ up
    Branch my_branch set up to track remote branch my_branch from origin.
    $ git pull
    

    And you're good to go

    0 讨论(0)
提交回复
热议问题