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

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

    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.

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

    You can set up a really good alias that can handle this without the overly verbose syntax.

    I have the following alias in ~/.gitconfig:

    po = "!git push -u origin \"$(git rev-parse --abbrev-ref HEAD)\""
    

    After making a commit on a new branch, you can push your new branch by simply typing the command:

    git po
    
    0 讨论(0)
  • 2020-11-22 09:21

    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"
    
    0 讨论(0)
  • 2020-11-22 09:22

    If the below doesn't work:

    git config --global push.default current
    

    You should also update your project's local config, as its possible your project has local git configurations:

    git config --local push.default current
    
    0 讨论(0)
  • 2020-11-22 09:23

    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 branch.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.

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

    This is my most common use for The Fuck.

    $ git push
    fatal: The current branch master has no upstream branch.
    To push the current branch and set the remote as upstream, use
    
        git push --set-upstream origin master
    
    $ fuck
    git push --set-upstream origin master [enter/↑/↓/ctrl+c]
    Counting objects: 9, done.
    ...
    

    Also, it's fun to type swear words in your terminal.

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