سؤال

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>.push sets a default refspec for pushing if no other refspec is specified. By default, doing git push origin will 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:

git config remote.origin.push refs/heads/master:refs/heads/master

... you will find that git push origin will just push master to master.

The other command you quote, git push -u origin master, sets two different config options if the push is successful:

  • branch.master.remote is set to origin
  • branch.master.merge is set to refs/heads/master

These essentially say that master in origin should be regarded as the default "upstream" branch of your master branch. Their most obvious effect is to provide a default action for git pull when you are on master, but are also used in a variety of other situations, such as providing the helpful message at the top of git status that tells you where master is compared to origin/master. These settings are not, however, used to inform the default action of git push and git push origin unless you have also set the config option push.default to tracking (or upstream in recent versions).

So, as a very approximate summary, setting remote.<name>.push affects the default action of git push, while git push -u origin master sets config options that usually just affect the action of git pull.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top