Search code examples
gitgithubgit-pushgit-pull

Why set a remote tracking branch (aka upstream) when we are already specifying the remote while pulling?


Based on the other answers and the documentation, I understand that setting a remote tracking branch tells git where to pull from and where to push to. But am I not telling git that every time I perform git pull?

For example with git pull origin master I am telling git to pull from the remote named origin and the branch master on that remote. What is the use of having a remote tracking branch in this case?

I do know it determines which remote branch git status reports that my current local branch is ahead of or behind.

I am aiming to create a setup where I am pulling from the original branch and pushing to its fork on my account. So far all I can think of is setting up 2 remotes. One to the original repository (upstream) and one to my fork (origin). Thus I can git pull upstream master for pulling the lastest code and git push origin master to push my modifications.

It doesn't seem like setting a remote tracking branch serves any real purpose.


Solution

  • It serves as the default. For most people in most cases, they are generally pulling and pushing from one remote repo, and less often or never from another.

    For example, I have a fork of my father's GitHub repo, DomingoMontoya/sword-designs.git on my account: InigoMontoya/sword-designs.git. On my laptop, I cloned the latter, which git clone very conveniently automatically names origin and sets up as the remote tracking branch for my local clone. This means that as I do my work, I can very conveniently push commits to my GitHub repo with two words: git push. I don't have to remember to type git push origin, or remember whether I named my default remote origin or github or upstream.

    Sometimes I have a design idea while traveling in search of the Six-Fingered Man, and I push it from the clone on my phone to my GitHub repo. When I get home I just type 'git pull'. Also my friend Fezzik sometimes contributes ideas via Pull Request to , and once merged I also git pull them to my local clone.

    Once in a while I git fetch upstream to get the latest from my father's repo, but sadly nothing ever comes down because he is dead 😢. I do it anyway as a way to honor his memory. Or maybe I have some illogical hope that he really isn't dead.

    From git help remote:

    Note that the push URL and the fetch URL, even though they can be set differently, must still refer to the same place. What you pushed to the push URL should be what you would see if you immediately fetched from the fetch URL. If you are trying to fetch from one place (e.g. your upstream) and push to another (e.g. your publishing repository), use two separate remotes.