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)所评论的那样,如果当地分支机构还不存在的话。如果是这样,你就需要
首先,一些背景:跟踪意味着本地分支的上游设置为远程分支:
1 2 | # git config branch.<branch-name>.remote origin # git config branch.<branch-name>.merge refs/heads/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. and.remote branch. configuration entries) so that.merge git 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 .
号
而
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显示
git status 和git branch -v 两个分支之间的关系。 - 指示
git pull 在新分支被签出时,不带参数从上游退出。
请参见"如何使现有Git分支跟踪远程分支?"更多信息。
完全没有区别!
1)江户十一〔6〕。
如果没有
实际上,1)的行为类似于
2)江户十一〔12〕。
"为方便起见",没有
实际上,2)的行为类似于
如你所见:没有区别。
但它会变得更好:
3)江户十一〔36〕号
如果"分支"还不存在,但"来源/分支"1存在,则等同于
所有三个命令都将"branch"的"upstream"设置为"origin/branch"(或失败)。
上游被用作参数的参考点,减去
例如,
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完成时,可以为您拔出
不能用此命令创建新分支
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 |
。
但是,您可以使用
1 2 3 | $ git checkout -b new-branch Switched to a new branch 'new-branch' M src/App.js |
。