1

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:

  • Tracking branch named "master" in my local machine (tracks origin/master).
  • Remote tracking branch named "origin/master" in my local machine.
  • Remote branch named "master" in remote repository"

Is this correct?

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

Thanks in advance.

Qwerty
  • 648
  • 8
  • 21

1 Answers1

3

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).

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • Hi. Is point #4 about "git push -u" command also correct? Is it true that Git will make the not-tracking branch to a tracking branch which tracks "origin/master"? – Qwerty May 16 '15 at 13:28
  • So is it true to say that we have two types of tracking branches: one that can track local branches (master -> origin/master) and other that can track remote branches (origin/master -> remote master)? – Qwerty May 16 '15 at 13:39
  • @Qwerty in a sense yes, but you don't create a special type of local `master` branch. `master` remains `master`. It only has extra attributes (`remote` and `merge`) attached to it, which makes it have an upstream branch. `origin/master` is the remote tracking branch. You can delete `master` and still have the remote tracking branch `origin/master` in your repo. – VonC May 16 '15 at 13:42
  • So the only reason to use a tracking branch (use the -u option), is to make Git remember to which remote branch it should push the code on next push. Correct? – Qwerty May 16 '15 at 13:46
  • @Qwerty yes, by default a local branch doesn't know where to push to. And a Git repo wouldn't know from where to merge from (during a `git pull`) – VonC May 16 '15 at 13:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/77966/discussion-between-qwerty-and-vonc). – Qwerty May 16 '15 at 13:54