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
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 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 '
(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:
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.
git pull, whereas some branches would ask for a remote branch to pull from. It turns out that if you, in your first-time, are checking out a remote branch that your peer created, git goes on and adds
branch.<BNAME>.remote=originto the local gitconfig. Which then allows you to issue
git pull. However, if you are the one creating the branch
git checkout -b BNAME, then git -of course- does not know. So you should specify its remote - batilc 2016-12-23 07:56
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 checkout -b <branch> --track <remote>/<branch>- VonC 2016-12-23 08:00
alwayssimply performs what we are talking about. This setting defaults to
true, which means the tracking will be performed only when checking out a remote branch.
truedoes not setup tracking for locally created branches - batilc 2016-12-26 06:39
--trackoption while having this
autosetupmergesetting on? My guess would be that your explicit configuration should be overriding the
autosetupmergeoption. Just asking if you have tried it alread - batilc 2016-12-26 06:49
There is no difference at all!
git checkout -b branch origin/branch
If there is no
--track and no
--track is assumed as default. The default can be changed with the setting
In effect, 1) behaves like
git checkout -b branch --track origin/branch.
git checkout --track origin/branch
“As a convenience”,
-b and the argument to
-b is guessed to be “branch”. The guessing is driven by the configuration variable
In effect, 2) behaves like
git checkout -b branch --track origin/branch.
As you can see: no difference.
But it gets even better:
git checkout branch
is also equivalent to
git checkout -b branch --track origin/branch if “branch” does not exist yet but “origin/branch” does1.
All three commands set the “upstream” of “branch” to be “origin/branch” (or they fail).
Upstream is used as reference point of argument-less
git merge and thus
git pull (if configured like that (which is the default or almost the default)).
git status tells you how far behind or ahead you are of upstream, if one is configured.
git push is configured to push the current branch upstream by default2 since git 2.0.
1 ...and if “origin” is the only remote having “branch”
2 the default (named “simple”) also enforces for both branch names to be equal
The book seems to indicate that those commands yield the same effect:
The simple case is the example you just saw, running git checkout -b [branch] [remotename]/[branch]. If you have Git version 1.6.2 or later, you can also use the --track shorthand:
$ git checkout --track origin/serverfix Branch serverfix set up to track remote branch serverfix from origin. Switched to a new branch 'serverfix'
To set up a local branch with a different name than the remote branch, you can easily use the first version with a different local branch name:
$ git checkout -b sf origin/serverfix
That's particularly handy when your bash or oh-my-zsh git completions are able to pull the
origin/serverfix name for you - just append
-t) and you are on your way.