Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'?

like image 817
Phillip Avatar asked Jul 08 '11 22:07

Phillip


People also ask

What is meant by tracking in git?

Tracking branches are local branches that have a direct relationship to a remote branch. If you're on a tracking branch and type git pull , Git automatically knows which server to fetch from and which branch to merge in.

How does git keep track of what is currently checked out?

The git checkout command lets you navigate between the branches created by git branch . Checking out a branch updates the files in the working directory to match the version stored in that branch, and it tells Git to record all new commits on that branch.

What is the difference between git pull and git fetch?

Git Fetch is the command that tells the local repository that there are changes available in the remote repository without bringing the changes into the local repository. Git Pull on the other hand brings the copy of the remote directory changes into the local repository.

How do you see which remote a branch is tracking?

You can tell Git to track the newly created remote branch simply by using the -u flag with "git push".


1 Answers

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.

like image 192
Mark Longair Avatar answered Sep 27 '22 19:09

Mark Longair