358

I have two different versions of git. In the 1.6.2 version, git push does not have the -u option. It only appears in the 1.7.x version.

From the docs, the -u is related to the variable

branch.<name>.merge

in git config. This variable is described below:

Defines, together with branch.<name>.remote, the upstream branch 
for the given branch. It tells git fetch/git pull which branch to merge.

What is an upstream branch ?

ZoogieZork
  • 11,215
  • 5
  • 45
  • 42
Frankie Ribery
  • 11,933
  • 14
  • 50
  • 64
  • 1
    See also http://stackoverflow.com/questions/2739376/definition-of-downstream-and-upstream/2749166#2749166 – VonC Apr 06 '11 at 04:49

3 Answers3

411

"Upstream" would refer to the main repo that other people will be pulling from, e.g. your GitHub repo. The -u option automatically sets that upstream for you, linking your repo to a central one. That way, in the future, Git "knows" where you want to push to and where you want to pull from, so you can use git pull or git push without arguments. A little bit down, this article explains and demonstrates this concept.

Melebius
  • 6,183
  • 4
  • 39
  • 52
Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
  • 20
    I see that the article you've linked to does point this out, but since it's something of a gotcha, I think it's worth pointing out that the branch which is pushed to with `git push` [isn't affected by the upstream branch configuration](http://longair.net/blog/2011/02/27/an-asymmetry-between-git-pull-and-git-push/) unless you have `push.default` set to `tracking` (or `upstream` in later versions of git). – Mark Longair Apr 22 '11 at 11:59
  • i dont understand why eclipse egit offers both then? – HaveAGuess Dec 05 '12 at 22:22
  • 27
    @HaveAGuess Probably for the same reason that Eclipse also offers both misery and despair. – twiz Apr 17 '15 at 02:34
16

When you push a new branch the first time, use:

git push -u origin <branch>

After that, you can just type a shorter command:

git push

The first-time -u option created a persistent upstream tracking branch with your local branch.

Melebius
  • 6,183
  • 4
  • 39
  • 52
Julian Pechacek
  • 160
  • 1
  • 4
11

This is no longer up-to-date!

Push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.

Since Git 2.0, Git defaults to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.

The -u option does the following: For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull and other commands. So, after pushing your local branch with -u option, this local branch will be automatically linked with remote branch, and you can use git pull without any arguments.

Bergrebell
  • 4,263
  • 4
  • 40
  • 53
  • 2
    But what does flag `-u` stand for now? It is still recommended by GitHub to use this flag when we create a new repo... – Jean Paul Oct 17 '17 at 09:24
  • 11
    @JeanPaul - The -u option does the following: For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull and other commands. So, after pushing your local branch with -u option, this local branch will be automatically linked with remote branch, and you can use git pull without any arguments. – Bergrebell Oct 18 '17 at 07:43
  • thanks for the hint @Melebius - i've updated the answer accordingly. – Bergrebell Jan 31 '23 at 15:24