13

I run 'git branch -r' and get

origin/branch1

origin/branch2

From the man page, the -r option will "list or delete (if used with -d) the remote-tracking branches". So origin/branch1 and origin/branch2 are known as remote-tracking branches. However, you can't commit directly onto a remote-tracking branch (an anonymous branch will be created instead). A remote-tracking branch simply tracks a remote branch when running 'git fetch'.

Here's where the semantics get a little blurry for me. If I then

git checkout -b branch1 origin/branch1

I get the following output: "Branch branch1 set up to track remote branch branch1 from origin. Switched to a new branch 'branch1'"

Here's my question, put as verbosely as possible to clarify what's confusing me... By virtue of having branch1 set up to track remote branch branch1 from origin, is 'branch1' thus considered a remote-tracking branch? If so, doesn't this conflict with the fact that 'origin/branch1' was already listed as a remote tracking branch when running 'git branch -r'? From what I understand, there exist either local (topic) branches or remote-tracking branches. When running 'git checkout -b branch1 origin/branch1', am I creating a local (topic) branch (onto which I can add commits) that is tracking a remote branch by way of fetches? Running 'git branch' now gives: '* branch1', and running 'git branch -r' still gives 'origin/branch1' and 'origin/branch2'. I created branch1 to add commits to and to track origin/branch1. Which is considered the remote-tracking branch, 'branch1' from the output of 'git branch', or 'origin/branch1' from the output of 'git branch -r'?

Community
  • 1
  • 1
Phillip
  • 133
  • 4

1 Answers1

14

This is a good question about a particularly annoying bit of git terminology, albeit one that the project seems to be slowly fixing.

Basically, "track" means something very different in the expressions (a) "remote-tracking branch" and (b) "branch1 set up to track remote branch branch1 from origin". Here's a quick summary:

  1. "remote-tracking branch": remote-tracking branches are branches that are usually updated by git fetch, and, consequently, git pull.¹ You can think of these as like a cache of the state of the branch in the remote repository. You can merge from them, examine their history, etc. but you can't work directly on them. "Track" in this phrase means that the remote-tracking branch represents the state of the branch in the remote repository the last time that remote-tracking branch was updated.
  2. Branch foo set up to track remote branch bar from origin: in this phrase what you're being told is that git has set up configuration variables that associate your local branch foo with the remote-tracking branch origin/bar. This enables nice features like being able to just type git pull while you're on branch foo in order to fetch and then merge from origin/bar. It's also how you get helpful the messages about the state of your branch relative to the remote-tracking branch, like "Your branch foo is 24 commits ahead of origin/bar can can be fast-forwarded". You're being told that your local branch is tracking has been associated with a remote-tracking branch. You also hear this referred to as origin/bar being upstream with respect to foo.

So, these senses of track / tracking are quite different, and sadly it's a common source of confusion.

The second sense seems to be being slowly deprecated, however - for example, one of the possible options for push.default used to be tracking, but this is now deprecated in favour of the option name upstream.


So, to answer your questions directly:

By virtue of having branch1 set up to track remote branch branch1 from origin, is 'branch1' thus considered a remote-tracking branch?

No, branch1 is not a remote-tracking branch.

When running 'git checkout -b branch1 origin/branch1', am I creating a local (topic) branch (onto which I can add commits) that is tracking a remote branch by way of fetches?

Well, sort of - it's tracking (sense 2) a remote-tracking branch, and the latter is updated from a branch in the remote repository by fetches. (Personally, I try to avoid the term "remote branch", in favour of "the branch in the remote repository", just in case people think you mean a remote-tracking branch.)

Running 'git branch' now gives: '* branch1', and running 'git branch -r' still gives 'origin/branch1' and 'origin/branch2'. I created branch1 to add commits to and to track origin/branch1. Which is considered the remote-tracking branch, 'branch1' from the output of 'git branch', or 'origin/branch1' from the output of 'git branch -r'?

The remote-tracking branch is origin/branch1.


¹ They're also updated when you make a successful git push to the corresponding branch in the remote repository.

Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • 1
    That truly helped clarify things, thanks Mark! So the trick is to see it in the sense that the local editable branch created is 'tracking' (or more appropriately, 'associated with') the remote repository's branch, and that a remote-tracking branch reflects only what is on the remote end and _cannot_ be edited. Thanks for helping affirm my suspicion that there are indeed two meanings to the term 'track' in git. – Phillip Jul 08 '11 at 23:43
  • 'remote (tracking) branch' is what we get in our local repository; while 'the branch in the remote repository' is something in a remote repository, though the former, by nature, tracks the latter. so: (A) I think we shouldn't mix them; (B) In the name 'remote (tracking) branch', 'tracking' shouldn't be used to avoid confusion with local tracking branches. – ryenus May 25 '12 at 07:03