8

It seems that I can pull from, or push to, a branch on a remote repository, from/to the branch that I am working on. If so, then what is the purpose of a remote tracking branch?

Is it solely for the purpose of checking out the branch and seeing what it looks like? It appears that a remote tracking branch is like a mirror to a branch on a remote.

FYI: I'm fairly new to git, but have read and re-read many tutorials, but I am still no more clear on this point!

Thanks!

cammil
  • 9,499
  • 15
  • 55
  • 89

1 Answers1

8

You're right - remote branches do indeed mirror branches in the remote repository.

Remote branch and remote tracking branch both are used to refer to a branch of the form refs/remotes/<remote-name>/<branch-name>, reported e.g. as origin/master. (Note that this is sometimes confused with the notion of a branch which is tracking a remote branch, e.g. your master branch is associated with origin/master. The terminology is unfortunate, but there we are.)

The purpose of a remote tracking branch is to remember the last known position of the branch in the remote repository. This is necessary in order for git pull to work; it fetches from the branch on the remote (origin's master branch), stores that in the remote tracking branch (origin/master), and then merges that locally. Commits can only be created locally, and merges can only be done in a work tree, so this is completely essential!

The remote tracking branch is also useful, as you mention, for examination of what's going on in the remote repository. By default there are remote branches for all of the remote's branches, so you can easily use git remote update [--prune] <remote> or git fetch <remote> to update them, and then check them out and play with them as you like. Note that you can do things besides check them out - you can diff against them (git diff origin/master), find out what commits origin has that you don't (git log master..origin/master), or anything else you like. Since all examinations of history are local, the remote branch has to be there for you to use.

Since git push affects branches in the remote, it naturally updates your remote tracking branches; it wouldn't be very intelligent to change origin's master then pretend it was still in the old position until you fetched! But it doesn't actually depend on the remote tracking branches. (If you set push.default to tracking, it will use your tracking configuration, e.g. master tracking origin's master, to decide what to push. But it still doesn't actually depend on the remote tracking branch.)

Cascabel
  • 479,068
  • 72
  • 370
  • 318