Question
What’s the difference between:
$ git remote add origin git@github.com:yourname/yourproject.git
$ git config remote.origin.push refs/heads/master:refs/heads/master
$ git push
and:
$ git remote add origin git@github.com:yourname/yourproject.git
$ git push origin master -u
Is the second version simply newer and shorter than the first version, or are there other differences?
Background Research
As of Git 1.7.0, you can use the --set-upstream option with git push. According to the git push manual:
-u, --set-upstream For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands. For more information, see branch.<name>.merge in git-config(1).
No, these are very different. The first config setting,
remote.<name>.pushsets a default refspec for pushing if no other refspec is specified. By default, doinggit push originwill push every branch to a branch with a matching name so long as a branch with that name already existed on the remote. After doing:… you will find that
git push originwill just pushmastertomaster.The other command you quote,
git push -u origin master, sets two different config options if the push is successful:branch.master.remoteis set tooriginbranch.master.mergeis set torefs/heads/masterThese essentially say that
masterinoriginshould be regarded as the default “upstream” branch of yourmasterbranch. Their most obvious effect is to provide a default action forgit pullwhen you are onmaster, but are also used in a variety of other situations, such as providing the helpful message at the top ofgit statusthat tells you wheremasteris compared toorigin/master. These settings are not, however, used to inform the default action ofgit pushandgit push originunless you have also set the config optionpush.defaulttotracking(orupstreamin recent versions).So, as a very approximate summary, setting
remote.<name>.pushaffects the default action ofgit push, whilegit push -u origin mastersets config options that usually just affect the action ofgit pull.