git checkout --track origin/branch 和 git checkout -b branch origin/branch 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10002239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between git checkout --track origin/branch and git checkout -b branch origin/branch
提问by yorch
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??
有什么实际区别吗??
Thanks!
谢谢!
回答by VonC
The two commands have the same effect (thanks to Robert Siemer's answer for pointing it out).
这两个命令具有相同的效果(感谢 Robert Siemer 的回答指出了这一点)。
The practical difference comes when using a local branch named differently:
使用不同命名的本地分支时会出现实际差异:
git checkout -b mybranch origin/abranch
will createmybranch
and trackorigin/abranch
git checkout --track origin/abranch
will only create 'abranch
', not a branch with a different name.
git checkout -b mybranch origin/abranch
将创建mybranch
和跟踪origin/abranch
git checkout --track origin/abranch
只会创建“abranch
”,而不是具有不同名称的分支。
(That is, as commentedby Sebastian Graf, if the local branch did notexist already.
If it did, you would need git checkout -B abranch origin/abranch
)
(也就是说,正如Sebastian Graf评论的那样,如果本地分支不存在。
如果存在,您将需要git checkout -B abranch origin/abranch
)
Note: with Git 2.23 (Q3 2019), that would use the new command git switch
:
注意:对于 Git 2.23(2019 年第三季度),将使用新命令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.defaultRemote
configuration 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=origin
to always checkout remote branches from there if<branch>
is ambiguous but exists on the 'origin' remote.
如果分支存在于多个遥控器中并且其中一个由
checkout.defaultRemote
配置变量命名,我们将使用该分支来消除歧义,即使它<branch>
在所有遥控器中都不是唯一的。
将其设置为例如checkout.defaultRemote=origin
,如果<branch>
不明确但存在于“原点”遥控器上,则始终从那里签出远程分支。
Here, '-c
' is the new '-b
'.
这里,' -c
' 是新的 ' -b
'。
First, some background: Trackingmeans 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:
git checkout -b branch origin/branch
将要:
- create/reset
branch
to the point referenced byorigin/branch
. - create the branch
branch
(withgit branch
) and track the remote tracking branchorigin/branch
.
- 创建/重置
branch
为引用的点origin/branch
。 - 创建分支
branch
(使用git branch
)并跟踪远程跟踪分支origin/branch
。
When a local branch is started off a remote-tracking branch, Git sets up the branch (specifically the
branch.<name>.remote
andbranch.<name>.merge
configuration entries)so thatgit pull
will appropriately merge from the remote-tracking branch.
This behavior may be changed via the globalbranch.autosetupmerge
configuration flag. That setting can be overridden by using the--track
and--no-track
options, and changed later using git branch--set-upstream-to
.
当本地分支从远程跟踪分支启动时,Git 会设置分支(特别是
branch.<name>.remote
和branch.<name>.merge
配置条目),以便git pull
从远程跟踪分支适当地合并。
可以通过全局branch.autosetupmerge
配置标志更改此行为。可以使用--track
和--no-track
选项覆盖该设置,稍后使用 git branch 进行更改--set-upstream-to
。
And git checkout --track origin/branch
will do the same as git branch --set-upstream-to
):
并且git checkout --track origin/branch
会做与 ) 相同的事情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
'.
它还将为“ 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)
(注意:git1.8.0 将弃用git branch --set-upstream
并替换为git branch -u|--set-upstream-to
:参见git1.8.0-rc1 公告)
Having an upstream branch registered for a local branch will:
为本地分支注册上游分支将:
- tell git to show the relationship between the two branches in
git status
andgit branch -v
. - directs
git pull
without argumentsto pull from the upstream when the new branch is checked out.
- 告诉 git显示
git status
和 中两个分支之间的关系git branch -v
。 - 指导
git pull
不带参数从上游拉当新分支签出。
See "How do you make an existing git branch track a remote branch?" for more.
有关更多信息,请参阅“如何使现有的 git 分支跟踪远程分支?”。
回答by Robert Siemer
There is no difference at all!
根本没有区别!
1) git checkout -b branch origin/branch
1) git checkout -b branch origin/branch
If there is no --track
and no --no-track
, --track
is assumed as default. The default can be changed with the setting branch.autosetupmerge
.
如果没有--track
和没有--no-track
,--track
则假定为默认值。可以通过设置更改默认值branch.autosetupmerge
。
In effect, 1) behaves like git checkout -b branch --track origin/branch
.
实际上, 1) 的行为类似于git checkout -b branch --track origin/branch
。
2) git checkout --track origin/branch
2) git checkout --track origin/branch
“As a convenience”, --track
without -b
implies -b
and the argument to -b
is guessed to be “branch”. The guessing is driven by the configuration variable remote.origin.fetch
.
“为方便起见”,--track
没有-b
隐含-b
,参数 to-b
被猜测为“分支”。猜测是由配置变量驱动的remote.origin.fetch
。
In effect, 2) behaves like git checkout -b branch --track origin/branch
.
实际上, 2) 的行为类似于git checkout -b branch --track origin/branch
。
As you can see: no difference.
如您所见:没有区别。
But it gets even better:
但它变得更好:
3) git checkout branch
3) git checkout branch
is also equivalent to git checkout -b branch --track origin/branch
if “branch” does not exist yet but “origin/branch” does1.
也等价于git checkout -b branch --track origin/branch
if “branch” 尚不存在但 “origin/branch” 存在1。
All three commands set the “upstream” of “branch” to be “origin/branch” (or they fail).
所有三个命令都将“branch”的“upstream”设置为“origin/branch”(否则失败)。
Upstream is used as reference point of argument-less git status
, git push
, git merge
and thus git pull
(if configured like that (which is the default or almost the default)).
上游被用作参考点的参数少git status
,git push
,git merge
并因此git pull
(如果配置这样的(这是默认值或几乎默认))。
E.g. git status
tells you how far behind or ahead you are of upstream, if one is configured.
例如git status
,如果已配置,则告诉您上游落后或领先多远。
git push
is configured to push the current branch upstream by default2since git 2.0.
git push
从 git 2.0 开始,默认配置为将当前分支推送到上游2。
1...and if “origin” is the only remote having “branch”
2the default (named “simple”) alsoenforces for both branch names to be equal
1...如果“origin”是唯一具有“branch”的远程
2默认(名为“simple”)也强制两个分支名称相等
回答by Pat
The bookseems 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 -b [branch] [remotename]/[branch]。如果您有 Git 版本 1.6.2 或更高版本,您还可以使用 --track 速记:
$ 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 --track
(or -t
) and you are on your way.
当您的 bash 或 oh-my-zsh git 完成能够origin/serverfix
为您提取名称时,这特别方便- 只需附加--track
(或-t
),您就可以开始了。
回答by Green
You can't create a new branch with this command
您无法使用此命令创建新分支
git checkout --track origin/branch
if you have changes that are not staged.
如果您有未上演的更改。
Here is example:
这是示例:
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: src/App.js
no changes added to commit (use "git add" and/or "git commit -a")
// TRY TO CREATE:
$ git checkout --track origin/new-branch
fatal: 'origin/new-branch' is not a commit and a branch 'new-branch' cannot be created from it
However you can easily create a new branch with un-staged changes with git checkout -b
command:
但是,您可以使用以下git checkout -b
命令轻松创建具有未暂存更改的新分支:
$ git checkout -b new-branch
Switched to a new branch 'new-branch'
M src/App.js