Reputation: 199
This is my understanding about tracking git branches, please correct me if I am missing something here. To track a remote branch, you would first run git fetch origin
, which brings a copy image of the remote git repository. Let's say you list the branches you want to create a tacking branch to mybranch
, you'd simply run git branch mybranch
and then git will automatically create a remote branch and make it tracking the corresponding (name correlation) remote branch. Very simple!
The question is, why do most of the git folks talk about the option --track
if git is capable to identify the branch you just create (based on name correlation) is available in origin
or remote repository?
Upvotes: 2
Views: 3055
Reputation: 487805
There are some other details that are not quite right but the key is here:
you'd simply run
git branch mybranch
and then git will ... make it tracking the corresponding (name correlation) remote branch
When you use git branch mybranch
to create a branch, it doesn't do that. It does do that, at least by default, if you run git branch mybranch origin/mybranch
.
If you use git branch mybranch
, though, Git creates mybranch
based on the current commit hash, as if you had done:
git branch mybranch $(git rev-parse HEAD)
and the default behavior when naming a remote-tracking branch is controlled by the configuration setting in branch.autoSetupMerge
.
The git checkout
command will also sometimes, but not always, create a new branch using an implied --track
and remote-tracking branch. Specifically, given git checkout name
, there are numerous optional paths, depending on whether name
can be resolved:
name
is the name of an existing branch: git checkout
tries to switch to that existing branch.name
is the name of an existing tag, or otherwise resolves to a commit through the multi-step procedure described in the gitrevisions documentation: git checkout
tries to check out that particular commit as a detached HEAD.name
is not any of the above, but there exists exactly one remote-tracking branch which, if stripped of its remote, matches name
(e.g., you enter git checkout develop
when there is no develop
but there is an origin/develop
): in this case, Git will create a new branch develop
that "tracks" (has as its upstream) origin/develop
, just as git branch
would with the appropriate arguments and appropriate branch.autoSetupMerge
and/or branch.autoSetupRebase
settings.If all of the above fail, git checkout
tries interpreting name
as a path name. You can force git checkout
to skip the above steps (and go straight for the path name interpretation) using the --
option-terminator: git checkout -- master
means check out a file named master
and not a branch.
Upvotes: 5
Reputation: 45659
The automatic tracking you mention is a convenience "shorthand", but it doesn't always work.
Sometimes there are multiple remotes with the same branch name; in that case git doesn't know which one to track and won't automatically set up tracking info.
Also, sometimes you want a local branch to track a remote branch that doesn't have the same name.
Some developers might, I suppose, never need to explicitly set tracking info; but some do, so you can.
Upvotes: 2