git checkout --track origin/branch 和 git checkout -b branch origin/branch 的区别 [英] Difference between git checkout --track origin/branch and git checkout -b branch origin/branch
问题描述
有人知道这两个切换和跟踪远程分支的命令的区别吗?
git checkout -b branch origin/branchgit checkout --track 起源/分支
我认为两者都会跟踪远程分支,以便我可以将更改推送到原始分支,对吗?
是否有任何实际差异?
这两个命令的效果是一样的(感谢 Robert Siemer 的回答指出它).
实际区别在于使用名为 differently 的本地分支时:
git checkout -b mybranch origin/abranch
将创建mybranch
并跟踪origin/abranch
git checkout --track origin/abranch
只会创建abranch
",而不是具有不同名称的分支.
(即如Sebastian Graf 的评论,如果当地分支机构没有em> 已经存在.
如果是这样,您将需要 git checkout -B abranch origin/abranch
)
注意:对于 Git 2.23(2019 年第三季度),将使用 新命令 git switch
:
git switch -c --track <远程>/<分支>
<块引用>
如果分支存在于多个遥控器中并且其中一个由 checkout.defaultRemote
配置变量命名,我们将使用那个来消除歧义,即使 <branch>
在所有遥控器中并不是唯一的.
将其设置为例如checkout.defaultRemote=origin
如果
不明确但存在于origin"远程上,则始终从那里签出远程分支.
这里,'-c
' 是新的 '-b
'.
首先,一些背景:跟踪意味着本地分支将其上游设置为远程分支:
# git config branch..remote origin# git config branch..merge refs/heads/branch
<小时>
git checkout -b branch origin/branch
将:
- 创建/重置
branch
到origin/branch
引用的点. - 创建分支
branch
(使用git branch
) 并跟踪远程跟踪分支origin/branch
.
当本地分支从远程跟踪分支开始时,Git 会设置该分支(特别是 branch.
和 branch.<name>.merge
配置条目),以便 git pull
将适当地从远程跟踪分支合并.
可以通过全局 branch.autosetupmerge
配置标志更改此行为.可以使用 --track
和 --no-track
选项覆盖该设置,稍后使用 git branch --set-upstream-to代码>.
<小时>
并且 git checkout --track origin/branch
将与 git branch --set-upstream-to
):
# 或者,从 1.7.0 开始git branch --set-upstream 上游/分支分支# 或者,自 1.8.0(2012 年 10 月)git branch --set-upstream-to 上游/分支分支# 简短版本保持不变:git branch -u 上游/分支分支
它还将为branch
"设置上游.
(注意:git1.8.0 将弃用 git branch --set-upstream
并将其替换为 git branch -u|--set-upstream-to
:见git1.8.0-rc1 公告)
为本地分支注册上游分支将:
- 告诉git显示
git status
和git branch -v
中两个分支的关系. - 指示
git pull
不带参数在新分支检出时从上游拉取.
参见你好吗使现有的 git 分支跟踪远程分支?"了解更多信息.
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/abranch
will createmybranch
and trackorigin/abranch
git checkout --track origin/abranch
will 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.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.
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
branch
to 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>.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
.
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 status
andgit branch -v
. - directs
git pull
without 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 checkout --track origin/branch 和 git checkout -b branch origin/branch 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!