I did this:
git branch --track stats_page
to create a separate branch to try something out. But now everytime I push or pull I see:
$ git pull
From .
* branch master -> FETCH_HEAD
and
$ git push
Total 0 (delta 0), reused 0 (delta 0)
To .
ff0caf5..e360168 stats_page -> master
I’m far from a git master so I might be miss understanding what’s happening – but is my stats_page branch tracking to master?
I may have created the branch wrong – if so – can I fix it so that from now on it pushes and pulls to the remote stats_page branch? How?
Quick answer: The easiest way to fix this is in Make an existing Git branch track a remote branch?
What’s really going on here
I’ve always found descriptions of git’s “tracking branches” confusing myself. They sound, to me at least, like they do a lot more than they really do.
The trick, I think, is to realize that git doesn’t ever actually “track” a remote repository at all. (It does fetch from a remote repo.) Instead, a “tracking branch” tracks a branch in your own repo. To “track a remote branch”, you create a branch whose name says that it was originally copied from some other repo. (Under the covers, this means any branch stored under
refs/remotes/, more or less.) The most common “other repo” is the one namedorigin, which is the one that an initialgit clonesets up for you. As it says in the git-clone manual page:When you use
git branch --trackto create a branch, it makes the new branch “track” the “start point” that you give it. (And, “track” mainly means “associate for various push/pull/fetch actions, and tell me more when I rungit status.) So, assuming the repo you cloned-from already has astats_pagebranch, you probably meant to do this:which tells git “make new branch named
stats_pagethat tracks what I have inrefs/remotes/origin/stats_page” (you normally leave off therefs/headsandrefs/remotesparts, and git figures them out). You don’t even need the--trackhere, as theorigin/part on the right turns that on for you.Once you do all that, in git’s terminology, you have a “local” branch (
refs/heads/stats_page) that tracks a “remote” branch (refs/remotes/origin/stats_page). To actually update the “remote” branch you usegit fetch, or more explicitly,git fetch origin, which goes out to the URL associated withoriginand collects any new stuff and then adds it to your repo. Hence, despite calling this “remote”, it’s still remarkably local: it’s right there on your own local disk.(I have taken to calling git “the Borg of SCMs”: “We are the git-Borg. We will add your commit distinctiveness to our own.” Most of what you do just adds new stuff to your own repo-Borg-collective.)
Once you’ve used
git fetchto update your local copy of the “remote” branch (origin/stats_page), you can do agit mergeto get it merged in (tostats_page, the name without theorigin/part). And, agit pulljust runs those two in one easy step—which hides the fact that it’s thefetchthat actually gets new stuff from the “remote”Now, the problem you have is that when you ran:
you were on your
masterbranch, so you created a new “local” branch namedstats_pagethat tracks yourmaster, instead of trackingorigin/stats_page. Hence,git pullandgit pushjust pull and push on your local branch namedmaster. (Note: If you actually rungit fetch, that still fetches intoremotes/origin/*, so it still updatesorigin/stats_page, even though the one “local” branch is mistakenly tracking another “local” branch.)One last note: usually when you make a “local” branch that tracks a remote branch of the same name, you also want to do a
git checkoutimmediately. You can justgit checkout -b stats_page origin/stats_pageto get all three steps done at once. (If your version of git is old, it may not automatically track when you nameorigin/stats_pageon the right; but if so you probably should just update your version of git.)Footnote: Some people recommend against using
git pullat all, and I sort of agree with them.git pullreally mixes two very different operations. In particulargit pull remote_A remote_Bseems to surprise people when it does an octopus merge. Usinggit fetchnever seems to surprise people; and once they get used to, and familiar with, an explicitgit merge, the logic behind fast-forward merges makes a lot more sense. Still,git pullwith no arguments is so convenient….