简体   繁体   English

git checkout有什么区别 <branch_name> 和git checkout -b <branch_name>

[英]Is there any difference between git checkout <branch_name> and git checkout -b <branch_name>

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. 我是git的新手,所以我要进行拳头提交,因此为此我通过输入git checkout my_branch创建了一个分支。 But after I saw in the documentation that It is used git checkout -b my_branch 但是在文档中看到它使用git checkout -b my_branch

Is there any difference between those? 两者之间有什么区别吗?

Yes: 是:

$ git checkout asdfadsf
error: pathspec 'asdfasdf' did not match any file(s) known to git

This failed because I don't have a branch asdfasdf . 这失败了,因为我没有分支asdfasdf Git tried to treat asdfasdf as a file name, and I don't have a file named asdfasdf either. Git尝试将asdfasdf视为文件名,而且我也没有名为asdfasdf的文件。

$ git checkout -b asdfasdf
Switched to a new branch 'asdfasdf'

This succeeded and created a new branch. 这样成功并创建了一个新分支。

On the other hand, I don't have a branch named maint , and yet: 另一方面,我没有名为maint的分支,但是:

$ git checkout maint
Branch 'maint' set up to track remote branch 'maint' from 'origin'.
Switched to a new branch 'maint'

This, too, created a new branch, maint . 这也创建了一个分支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'. 它仍然说“ Switched to a new branch ,但首先说“ 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: 这背后的原因有些复杂,但可以归结为:

  • Without -b , if you ask for a branch that you don't have, Git will try some alternatives. 如果没有 -b ,那么如果您请求一个没有的分支,Git将尝试一些替代方法。 Some of them may work! 其中一些可能有效! When it works the way maint did here, the new branch has an upstream set already. 当按maint的方式工作时,新分支已经有了上游集。
  • With -b , Git will just create the branch, no questions asked (provided that creating a new branch is possible). 使用 -b ,Git将只创建分支,而不会提出任何问题(前提是可以创建一个新分支)。 The new branch won't be set up with an upstream. 新分支将不会设置上游。 If you already have the branch, you get an error. 如果您已经拥有分支,则会收到错误消息。

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 . 如果您想要一个带有上游集的分支(例如,如果有一个origin/feature/x123并且您想要创建自己的feature/x123进行匹配), 则不需要 -b选项,因为那样做不会搜索上游origin/feature/x123 If you don't want an upstream set, you do want the -b option. 如果不需要上游集, 需要-b选项。

(Whether and when you want the upstream set for you is a separate question. Search StackOverflow for the existing answers.) (是否以及何时需要您的上游集是一个单独的问题。在StackOverflow中搜索现有答案。)

when you run with -b you are telling git to create the branch for you. 当您使用-b运行时,您正在告诉git为您创建分支。 git checkout without -b requires the branch to exist already to work. 不带-b的git checkout要求分支已经存在才能正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM