0

On GitHub, they advice to do git push -u origin master, instead of git push.

What is the main difference ?

phd
  • 82,685
  • 13
  • 120
  • 165
jozinho22
  • 459
  • 2
  • 7
  • 24

1 Answers1

2

From man git-push:

   -u, --set-upstream
      For every branch that is up to date or successfully pushed, add
      upstream (tracking) reference ...

Basically, it means that it will not only upload your master to the master at origin, but also set up the local copy origin/master to be the tracking branch, that is the upstream of your local master.

This is usually already this way, so if that is the case, being in master and doing git push -u origin master will do nothing different from a plain git push. But if you misconfigured your repository, then sometimes the long version will fix it automatically.

Naturally, doing that command without thinking may result in bad situations, for example if you are in a public working branch and do that:

{mybranch} $ git push -u origin master

Now you uploaded your mybranch as master and set up origin/master as the upstream of mybranch, instead of the proper origin/mybranch, and you created quite a mess!

My guess is that GitHub recommends that for git newbies, to avoid calls to their customer support. But I would not recommend that as a default for pushing. In fact, if you misconfigure the upstream, a plain git push will fail with this message:

fatal: The current branch a has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin master

that tells you exactly what is happening and how to fix it (--set-upstream is a synonym of -u).

rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • I believed that `git push -u origin master` would have set `mybranch` upstream to `origin/master` but that doesn't seem to be the case in my test. Hence my question at: https://stackoverflow.com/questions/71784411/what-does-git-push-u-origin-branch-do-when-you-are-not-on-branch – PatS Apr 07 '22 at 21:19