First, you don't have to use git checkout
anymore. Since Git 2.23 (Aug. 2019), there is git switch
Second, you only have to use --track to set the upstream branch for a local branch, if the name of the upstream branch differs from the local one.
That assumes your local repo, in which you are creating a local branch:
- has a remote repository (generally referenced as origin)
- has fetched said remote repository (remote branches are fetched to your local repo)
By default, --track
is used automatically: from git switch
man page
If <branch>
is not found but there does exist a tracking branch in exactly one remote (call it <remote>
) with a matching name, treat as equivalent to
$ git switch -c <branch> --track <remote>/<branch>
So in that case, you don't even have to use --track
.
And if the remote branch does not yet exist, a git push -u origin yourBranch
would automatically set the upstream relationship between the local and remote branch. See "Why do I need to explicitly push a new branch?". Again, in that case, no need for --track
.