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

有人知道切换和跟踪远程分支的这两个命令之间的区别吗?

1
2
git checkout -b branch origin/branch
git checkout --track origin/branch

我认为两者都会跟踪远程分支,这样我就可以将更改推送到源站上的分支,对吗?

有什么实际的区别吗??

谢谢!


这两个命令有同样的效果(多亏了罗伯特·西默的回答)。

当使用一个不同名称的本地分支时,实际的区别就出现了:

  • git checkout -b mybranch origin/abranch将创建mybranch并跟踪origin/abranch
  • git checkout --track origin/abranch将只创建'abranch,而不是具有不同名称的分支。

(也就是说,正如塞巴斯蒂安·格拉夫(Sebastian Graf)所评论的那样,如果当地分支机构还不存在的话。如果是这样,你就需要git checkout -B abranch origin/abranch

首先,一些背景:跟踪意味着本地分支的上游设置为远程分支:

1
2
# git config branch.<branch-name>.remote origin
# git config branch.<branch-name>.merge refs/heads/branch

git checkout -b branch origin/branch将:

  • 创建/重置branchorigin/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..remote and branch..merge configuration entries) so that git pull will appropriately merge from the remote-tracking branch.
This behavior may be changed via the global branch.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 checkout --track origin/branchgit branch --set-upstream-to的作用相同:

1
2
3
4
5
6
 # 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

它还将为"EDOCX1"(7)设置上游。

(注:git1.8.0将取消对git branch --set-upstream的预测,代之以git branch -u|--set-upstream-to:见git1.8.0-rc1公告)

将上游分行注册为本地分行将:

  • 告诉Git显示git statusgit branch -v两个分支之间的关系。
  • 指示git pull在新分支被签出时,不带参数从上游退出。

请参见"如何使现有Git分支跟踪远程分支?"更多信息。


完全没有区别!

1)江户十一〔6〕。

如果没有--track--no-track,则假定--track为违约。默认值可以通过设置branch.autosetupmerge进行更改。

实际上,1)的行为类似于git checkout -b branch --track origin/branch

2)江户十一〔12〕。

"为方便起见",没有-b--track意味着-b-b的论点被认为是"分支"。猜测由配置变量remote.origin.fetch驱动。

实际上,2)的行为类似于git checkout -b branch --track origin/branch

如你所见:没有区别。

但它会变得更好:

3)江户十一〔36〕号

如果"分支"还不存在,但"来源/分支"1存在,则等同于git checkout -b branch --track origin/branch

所有三个命令都将"branch"的"upstream"设置为"origin/branch"(或失败)。

上游被用作参数的参考点,减去git statusgit pushgit merge,因此git pull(如果配置类似(这是默认值或几乎是默认值))。

例如,git status告诉您,如果配置了一个,您在上游的后面或前面有多远。

git push配置为自git 2.0以来默认2将当前分支推到上游。

1…如果"来源"是唯一有"分支"的远程设备2默认值(名为"简单")也强制两个分支名称相等。


这本书似乎表明,这些命令产生了同样的效果:

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:

1
2
3
$ 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:

1
$ git checkout -b sf origin/serverfix

当您的bash或oh my zsh git完成时,可以为您拔出origin/serverfix的名称—只需附加--track(或-t),然后您就可以上路了。


不能用此命令创建新分支

1
git checkout --track origin/branch

如果有未进行阶段化的更改。

以下是示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ 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

但是,您可以使用git checkout -b命令轻松地创建一个具有非阶段性更改的新分支:

1
2
3
$ git checkout -b new-branch
Switched to a new branch 'new-branch'
M       src/App.js