Are there different meanings to the concept of 'tracking' in git?

后端 未结 1 1498
余生分开走
余生分开走 2021-02-07 17:19

I run \'git branch -r\' and get

origin/branch1

origin/branch2

From the man page, the -r option will "list or delete (if used

1条回答
  •  囚心锁ツ
    2021-02-07 17:38

    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.

    0 讨论(0)
提交回复
热议问题