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

后端 未结 21 2023
南方客
南方客 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:33

    You can simply

    git checkout -b my-branch origin/whatever
    

    in the first place. If you set branch.autosetupmerge or branch.autosetuprebase (my favorite) to always (default is true), my-branch will automatically track origin/whatever.

    See git help config.

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

    Here is a bash alias for git push which is safe to run for every push and will automatically switch between setting upstream for the first push and then doing normal pushes after that.

    alias gpu='[[ -z $(git config "branch.$(git symbolic-ref --short HEAD).merge") ]] && git push -u origin $(git symbolic-ref --short HEAD) || git push'
    

    Original Post

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

    Because git has the cool ability to push/pull different branches to different "upstream" repositories. You could even use separate repositories for pushing and pulling - on the same branch. This can create a distributed, multi-level flow, I can see this being useful on project such as the Linux kernel. Git was originally built to be used on that project.

    As a consequence, it does not make assumption about which repo your branch should be tracking.

    On the other hand, most people do not use git in this way, so it might make a good case for a default option.

    Git is generally pretty low-level and it can be frustrating. Yet there are GUIs and it should be easy to write helper scripts if you still want to use it from the shell.

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

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

    $ git push -u origin HEAD
    
    0 讨论(0)
  • 2020-11-22 09:37

    I personally use these following alias in bash

    in ~/.gitconfig file

    [alias]
        pushup = "!git push --set-upstream origin $(git symbolic-ref --short HEAD)"
    

    and in ~/.bashrc or ~/.zshrc file

    alias gpo="git pushup"
    alias gpof="gpo -f"
    alias gf="git fetch"
    alias gp="git pull"
    
    0 讨论(0)
  • 2020-11-22 09:37

    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!

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