Does anybody know the difference between these two commands to switch and track a remote branch?
git checkout -b branch origin/branch
git checkout --track origin/branch
I think both keep track of the remote branch so I can push my changes to the branch on origin, right?
Are there any practical differences?
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:Here, ‘
-c‘ is the new ‘-b‘.First, some background: Tracking means that a local branch has its upstream set to a remote branch:
git checkout -b branch origin/branchwill:branchto the point referenced byorigin/branch.branch(withgit branch) and track the remote tracking branchorigin/branch.And
git checkout --track origin/branchwill do the same asgit branch --set-upstream-to):It would also set the upstream for ‘
branch‘.(Note: git1.8.0 will deprecate
git branch --set-upstreamand replace it withgit branch -u|--set-upstream-to: see git1.8.0-rc1 announce)Having an upstream branch registered for a local branch will:
git statusandgit branch -v.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.