git checkout< branch_name>之间有什么区别吗?和git checkout -b< branch_name> [英] Is there any difference between git checkout <branch_name> and git checkout -b <branch_name>
问题描述
我是git的新手,所以我要进行拳头提交,为此,我通过输入git checkout my_branch
创建了一个分支.但是在文档中看到它被使用后git checkout -b my_branch
I'm very new to git so I'm about to make my fist commit, so for this I have created a branch by typing git checkout my_branch
.This worked fine. But after I saw in the documentation that It is used git checkout -b my_branch
两者之间有什么区别吗?
Is there any difference between those?
推荐答案
是:
$ git checkout asdfadsf
error: pathspec 'asdfasdf' did not match any file(s) known to git
此操作失败,因为我没有有分支asdfasdf
. Git尝试将asdfasdf
当作文件名,而我也不也有一个名为asdfasdf
的文件.
This failed because I don't have a branch asdfasdf
. Git tried to treat asdfasdf
as a file name, and I don't have a file named asdfasdf
either.
$ git checkout -b asdfasdf
Switched to a new branch 'asdfasdf'
此操作成功并创建了一个新分支.
This succeeded and created a new branch.
另一方面,我没有有一个名为maint
的分支,但是:
On the other hand, I don't have a branch named maint
, and yet:
$ git checkout maint
Branch 'maint' set up to track remote branch 'maint' from 'origin'.
Switched to a new branch 'maint'
这也创建了一个 new 分支maint
.但是请注意它看起来有什么不同.它仍然显示Switched to a new branch
,但是 first 它显示Branch 'maint' set up to track remote branch 'maint' from 'origin'.
This, too, created a new branch, maint
. But notice how it looks different. It still says Switched to a new branch
, but first it says Branch 'maint' set up to track remote branch 'maint' from 'origin'.
其背后的原因有些复杂,但可以归结为:
The reasoning behind this is a little complicated, but it boils down to this:
- 没有
-b
,如果您请求一个没有的分支,Git将尝试一些替代方法.其中一些可能有效!当按maint
的方式工作时,新分支已经设置了上游. - 使用
-b
,Git会创建分支,而不会提出任何问题(前提是可以创建一个新分支).新分支不会设置为上游.如果您已经有分支机构,则会收到错误消息.
- Without
-b
, if you ask for a branch that you don't have, Git will try some alternatives. Some of them may work! When it works the waymaint
did here, the new branch has an upstream set already. - With
-b
, Git will just create the branch, no questions asked (provided that creating a new branch is possible). The new branch won't be set up with an upstream. If you already have the branch, you get an error.
如果您想要一个带有上游集的分支(例如,如果有一个origin/feature/x123
并且您想要创建自己的feature/x123
来进行匹配),则不要需要-b
选项,因为这不会搜索上游的origin/feature/x123
.如果您不想要上游集,则不想要-b
选项.
If you want a branch with an upstream set—for instance, if there's an origin/feature/x123
and you want your own feature/x123
created to match—you don't want the -b
option, because that won't do the search for an upstream origin/feature/x123
. If you don't want an upstream set, you do want the -b
option.
(是否以及何时需要您的上游集是一个单独的问题.在StackOverflow中搜索现有答案.)
(Whether and when you want the upstream set for you is a separate question. Search StackOverflow for the existing answers.)
这篇关于git checkout< branch_name>之间有什么区别吗?和git checkout -b< branch_name>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!