The two commands have the same effect (thanks to Robert Siemer’s answer for pointing it out).
The practical difference comes when using a local branch named differently:
git checkout -b mybranch origin/abranchwill createmybranchand trackorigin/abranchgit checkout --track origin/abranchwill only create ‘abranch‘, not a branch with a different name.
(That is, as commented by Sebastian Graf, if the local branch did not exist already.
If it did, you would need git checkout -B abranch origin/abranch)
Note: with Git 2.23 (Q3 2019), that would use the new command git switch:
git switch -c <branch> --track <remote>/<branch>
If the branch exists in multiple remotes and one of them is named by the
checkout.defaultRemoteconfiguration variable, we’ll use that one for the purposes of disambiguation, even if the<branch>isn’t unique across all remotes.
Set it to e.g.checkout.defaultRemote=originto always checkout remote branches from there if<branch>is ambiguous but exists on the ‘origin’ remote.
Here, ‘-c‘ is the new ‘-b‘.
First, some background: Tracking means that a local branch has its upstream set to a remote branch:
# git config branch.<branch-name>.remote origin
# git config branch.<branch-name>.merge refs/heads/branch
git checkout -b branch origin/branch will:
- create/reset
branchto the point referenced byorigin/branch. - create the branch
branch(withgit branch) and track the remote tracking branchorigin/branch.
When a local branch is started off a remote-tracking branch, Git sets up the branch (specifically the
branch.<name>.remoteandbranch.<name>.mergeconfiguration entries) so thatgit pullwill appropriately merge from the remote-tracking branch.
This behavior may be changed via the globalbranch.autosetupmergeconfiguration flag. That setting can be overridden by using the--trackand--no-trackoptions, and changed later using git branch--set-upstream-to.
And git checkout --track origin/branch will do the same as git branch --set-upstream-to):
# or, since 1.7.0
git branch --set-upstream upstream/branch branch
# or, since 1.8.0 (October 2012)
git branch --set-upstream-to upstream/branch branch
# the short version remains the same:
git branch -u upstream/branch branch
It would also set the upstream for ‘branch‘.
(Note: git1.8.0 will deprecate git branch --set-upstream and replace it with git branch -u|--set-upstream-to: see git1.8.0-rc1 announce)
Having an upstream branch registered for a local branch will:
- tell git to show the relationship between the two branches in
git statusandgit branch -v. - directs
git pullwithout arguments to pull from the upstream when the new branch is checked out.
See “How do you make an existing git branch track a remote branch?” for more.