Until recently, I have not been aware of --track switch for git branch. I read the documentation and tried this command, but it has no sense to me.
--trackWhen creating a new branch, set up
branch.<name>.remoteandbranch.<name>.mergeconfiguration entries to mark the start-point branch as "upstream" from the new branch. This configuration will tell git to show the relationship between the two branches ingit statusand gitbranch -v. Furthermore, it directsgit pullwithout arguments to pull from the upstream when the new branch is checked out.This behavior is the default when the start point is a remote-tracking branch. Set the branch.autoSetupMerge configuration variable to
falseif you wantgit checkoutandgit branchto always behave as if --no-track were given. Set it toalwaysif you want this behavior when the start-point is either a local or remote-tracking branch.
I can see that people relate to this switch when they want to make branch track upstream branch
What does it mean? Is it me or this switch description is confusing. When I use term upstream, I refer to the another remote repo (fork) that I can push changes to.
What happens when I start tracking remote branch? How does it manifest locally?
An upstream branch of a branch, or the tracked remote branch is simply the branch you will interact with by default when using
git pullandgit pushcommands.When pulling a branch into yours you can do it explicitly:
It will fetch the remote
originthen merge theorigin/the_branchinto your current branch.If you use to pull always the same branch, by setting an upstream branch, you can just launch
git pull:By default, when you start a new branch from a remote one, git will add it as the upstream branch:
When pushing, it's almost the same thing.
The config
push.defaultwill determine the default branch to push to when usinggit pushwith no parameters.With the value
upstream, it will simply push into the upstream branch.With the default value
simple, it will do the same but only if the local and upstream branch names are the same.I let you look at the doc to check other config possibilities.
You can see the the current upstream branches of all your branches by using the
-vvswitch:The upstream branch of a branch can also be referred with the
@{upstream}reference:The push branch as the equivalent
@{push}(it will be the same as@{upstream}in 99% of the use cases):The distinction between
@{upstream}and@{push}is for the cases when you use a triangular workflow: you pull from a read-only "upstream" project (usually a remote called by conventionupstream) and push to a writable repository.This is the case of the forking workflow used on GitHub.
I made a (french) blog post about this, here is the auto-translated version.