405

What does git --set-upstream do?

I tried to understand it by reading the git manual, but I didn't quite get it.

Zach
  • 467
  • 1
  • 2
  • 18

3 Answers3

501
git branch --set-upstream <remote-branch>

sets the default remote branch for the current local branch.

Any future git pull command (with the current local branch checked-out),
will attempt to bring in commits from the <remote-branch> into the current local branch.


One way to avoid having to explicitly type --set-upstream is to use its shorthand flag -u as follows:

git push -u origin local-branch

This sets the upstream association for any future push/pull attempts automatically.
For more details, checkout this detailed explanation about upstream branches and tracking.


To avoid confusion, recent versions of git deprecate this somewhat ambiguous --set-upstream option in favour of a more verbose --set-upstream-to option with identical syntax and behaviour

git branch --set-upstream-to <origin/remote-branch>
Community
  • 1
  • 1
TheCodeArtist
  • 19,131
  • 3
  • 60
  • 123
  • 20
    The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to – Rohit Singh Sengar Mar 09 '17 at 07:51
  • 1
    in this command `git push -u origin local-branch` what does the `origin` represent? Is there any case where I would type anything other than `origin` after the `-u` ? – John Henckel Mar 05 '18 at 19:02
  • 3
    @JohnHenckel `origin` refers to the remote git repository that was used to clone from. There can be [**multiple remote git repositories**](https://stackoverflow.com/questions/11690709/can-a-project-have-multiple-origins). In such a case, `origin` may be replaced with the proper name of the desired remote that one wishes to refer to. – TheCodeArtist Mar 06 '18 at 05:57
  • do a `git remote -v` to find your remotes, the default one is `origin` usually – xploreraj Aug 07 '18 at 04:00
57

When you push to a remote and you use the --set-upstream flag git sets the branch you are pushing to as the remote tracking branch of the branch you are pushing.

Adding a remote tracking branch means that git then knows what you want to do when you git fetch, git pull or git push in future. It assumes that you want to keep the local branch and the remote branch it is tracking in sync and does the appropriate thing to achieve this.

You could achieve the same thing with git branch --set-upstream-to or git checkout --track. See the git help pages on tracking branches for more information.

GOXR3PLUS
  • 5,939
  • 3
  • 30
  • 79
Will
  • 4,154
  • 1
  • 24
  • 45
13

git branch --set-upstream <<origin/branch>> is officially not supported anymore and is replaced by git branch --set-upstream-to <<origin/branch>>

Turbut Alin
  • 2,152
  • 1
  • 17
  • 28