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?
Is 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 create
git checkout --track origin/abranchwill only create '
abranch', not a branch with a different name.
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
branchto the point referenced by
- create the branch
git branch) and track the remote tracking branch
When a local branch is started off a remote-tracking branch, Git sets up the branch (specifically the
branch.<name>.mergeconfiguration entries) so that
git pullwill appropriately merge from the remote-tracking branch.
This behavior may be changed via the global
branch.autosetupmergeconfiguration flag. That setting can be overridden by using the
--no-trackoptions, and changed later using git branch
git checkout --track origin/branch will do the same:
# or, since 1.7.0 git branch --set-upstream branch upstream/branch # or, since 1.8.0 (October 2012) git branch --set-upstream-to branch upstream/branch # the short version remains the same: git branch -u branch upstream/branch
It would also set the upstream for '
(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 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.