What does '--set-upstream' do?

459,409

Solution 1

To avoid confusion,
recent versions of git deprecate this somewhat ambiguous --set-upstream option
in favor of a more verbose --set-upstream-to option
with identical syntax and behavior.
[ Reference ]


git branch --set-upstream-to <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 / --set-upstream-to 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.

Solution 2

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.

Solution 3

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

Solution 4

--set-upstream is used to map a branch in your local to a branch on remote so that you can just do git push or git pull and it will know which branch to push/pull from

For adding a remote repo I use these commands

  • First, check your remote repositories with git remote -v
  • If you can't see upstream then use git remote add upstream <URL>
  • Check again your remote repositories with git remote -v

One can have multiple remote's to their local repository and it can be added using the same commands above.

Just change the upstream name git remote add NAME <URL>

Solution 5

I'm assuming that your question is:

What does git push --set-upstream <repository> <branchname> do?

As you see, I assumed that the git command in question is git push. I hope that is what you meant. For simplifying the answer, I further specified that the local branch <branchname> that you are on has the same name as the remote branch on your upstream repository <repository> that you are pushing to. Finally, I assume a common git configuration.

With that said, this is my answer:

In addition to the operation that a git push without the option --set-upstream does, this option makes git push set at least two configuration variables:

  • branch.<branchname>.remote = <repository>
  • branch.<branchname>.merge = /ref/heads/<branchname>

That's all this command does. It stores upstream information (i.e., remote repository and branch) for the local branch in config variables.

Upstream information is stored under the local branch name. If your local branch is called main, the respective config variables are branch.main.remote and branch.main.merge. Based on the way how this upstream information is stored, a local branch can have no more than a single set of upstream information.

You can query whether any of these config variables are set using git config --get-regexp ^branch\.. This will output any variables that start with "branch."

The magic happens when these config variables are used by, e.g., git fetch, git pull or git push to figure out the upstream repository and remote branch for a local branch if you don't explicitly specify them on the commandline. That is, when these config variables are set, you can just issue git push and git will know (using these variables) the remote repository and upstream branch to use.

Suggested further reading:

But watch out for git quirks:

If <repository> is given as an URL or file path, see for example this example:

git push --set-upstream [email protected]:namespace/myproject.git master

git push does not create a reference to the remote branch head in .git/refs/remotes/<repository>

Only if the upstream repository has been given a name using

git remote add <repository> <URL>

and git push --set-upstream has been used with this name, the full power of remote tracking branches is available in all git commands.

Suggested further reading:

FYI: all commands tested with git V2.32 on Windows.

Share:
459,409
Евгений Масленков
Author by

Евгений Масленков

Updated on July 08, 2022

Comments

  • Евгений Масленков
    Евгений Масленков almost 2 years

    What does git --set-upstream do?

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

    • Daniel K.
      Daniel K. almost 3 years
      The question does not state the full git command. It can only be inferred that it's about the command git push --set-upstream.
  • John Henckel
    John Henckel over 6 years
    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 ?
  • TheCodeArtist
    TheCodeArtist over 6 years
    @JohnHenckel origin refers to the remote git repository that was used to clone from. There can be multiple remote git repositories. In such a case, origin may be replaced with the proper name of the desired remote that one wishes to refer to.
  • xploreraj
    xploreraj almost 6 years
    do a git remote -v to find your remotes, the default one is origin usually
  • Jim
    Jim over 5 years
    When I checkout with -t it does set the upstream for pushing, only for pulling.
  • T.Woody
    T.Woody over 3 years
    This answer is assuming there is a branch being pushed to :D