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!
Remote-tracking branches are references to the state of remote branches. They're local references that you can't move; Git moves them for you whenever you do any network communication, to make sure they accurately represent the state of the remote repository.
There is a command that gives you about all tracking branches. And to know about the pull and push configuration per branch you can use the command git remote show origin. and you can use -sb option for seeing the upstream.
When we usually think of branches, we think of collections of commits logically ordered in some way, but technically speaking a branch is just a pointer to a commit. The remote, which is really just short for remote repository, is a central place where branches and their commits are stored.
To create a new local branch based on a remote branch, use the "-track" option in the branch command. You can also do this by using the "checkout" command. If you want your local branch to have the same name as the remote branch, you only need to specify the name of the remote branch.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With