Qwerty
Qwerty

Reputation: 778

What is the purpose of "git push -u" command?

AFAIK, git does the following when entering the following command:

"git push -u origin master":

  1. Git checks if master branch is located in remote repo. If not it will create it.
  2. Git determines the diff between the local branch and the remote branch. Git updates the "diff" in the remote branch.
  3. Git will also create a new REMOTE TRACKING branch in the local machine named "origin/master" to track the remote branch.
  4. Git will set the non-tracking branch named "master" to be a tracking branch (in order to track "origin/master").

This will cause 3 branches to exist:

Is this correct?

Re-edit: assume the master branch exist in local machine.

Thanks in advance.

Upvotes: 1

Views: 894

Answers (1)

VonC
VonC

Reputation: 1324268

The master branch must exist before pushing.
That means at least one commit must have been done in a newly created repo (as I explained in "Why do I need to explicitly push a new branch?").

The remote tracking branch origin/master and the master branch on the remote upstream repo are then created.

To your point 4 (since master already exist), what is created is the association between the local branch master and an upstream branch (hence the -u or --upstream-to option) origin/master in .gitconfig:

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

From the discussion:

So If I can have master branch to track directly a remote branch, what benefit do I get in having:

"master" points to "origin/master" points to "remote master"?

What benefit I get in having this extra middleman branch?

You can indeed push directly to a remote branch with git push https://<login>@github.com/<login>/<repo> master:master.

The benefit of establishing formally a tracking relationship between a local branch master and a remote tracking one (origin/master) is to record where to push to (git push) or from where to merge from (git pull).

Upvotes: 3

Related Questions