36

When you have created a github-repo and added the the github-repo as remote

git remote add origin https://github.com/githubname/reponame.git

then you need to push your first commit with

git push -u origin master

I read (Why do I need to do `--set-upstream` all the time?) that this a short form for doing

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

What is a upstream exactly and why do I need to set it? There is little information about this on the net. I know that there is a similar topic What does 'git remote add upstream' help achieve?, but in my opinion it does not explain exactly what the upstream is and what git push -u origin master does, especially what is origin master pointing to, is it the local repo or the remote repo?

Community
  • 1
  • 1
patriques
  • 5,057
  • 8
  • 38
  • 48
  • See also this question: https://stackoverflow.com/questions/5697750/what-exactly-does-the-u-do-git-push-u-origin-master-vs-git-push-origin-ma – TuringTux Oct 25 '16 at 16:12

2 Answers2

53

In the command

git push -u origin master

The -u flag means that your local branch will become a tracking branch. That is, a branch that tracks a remote branch (the "upstream" branch), so that future git pull will know which branch to merge from and git push will be directed to the correct remote branch.

origin is the remote repository you are pushing to.

master is the refspec parameter. The refspec parameter specifies which local branch is pushed to what remote branch. It can be complicated, but in this case the short form master means to push the local master branch to the remote branch with the same name, origin/master.

Technically, the tracking adds the following information about the master branch to your .git/config:

[branch "master"]
    remote = origin
    merge = refs/heads/master

and it creates a file here .git/refs/remotes/origin/master, representing the remote branch.

Klas Mellbourn
  • 42,571
  • 24
  • 140
  • 158
  • Can you please add an explanation about what origin master is pointing to in the command $git push -u origin master and I will choose your answer as the resolving one. – patriques Jun 15 '13 at 09:52
  • 4
    The branch-refspec isn't that hard: `master` is short of `master:master` where the first "master" defines the local and the second "master" the remote branch :) – KingCrunch Jun 15 '13 at 11:53
2

"Upstream" is the repo you cloned (some of) the branches in yours from, and where you push changes to those branches (and optionally entire new branches) once they've been committed. GitHub acts as your upstream because they store the revisions for you, in a centralized location.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 6
    This is subtly wrong. Upstream is a relationship between branches, not repos. – millimoose Jun 15 '13 at 09:48
  • 6
    Not really. *Upstream*, in the sense "the thing that `--set-upstream` deals with", is not a relationship at the repo level at all. Different local branches can have their upstream set to branches in different remotes, or even to local branches. (E.g. feature branches that track your local `master`.) Saying "GitHub acts as your upstream" is just misleading since it conflates the meaning of the term in the context of a development workflow, and its meaning in Git parlance which the OP is asking about. – millimoose Jun 15 '13 at 10:05