“git branch”和“git checkout -b”有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7987687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:13:47  来源:igfitidea点击:

What is the difference between "git branch" and "git checkout -b"?

gitgit-branchgit-checkout

提问by Adrien Joly

I used git checkout -bto create a new branch. I think that git branchdoes the same thing. How do these two commands differ, if they differ at all?

我曾经git checkout -b创建一个新分支。我认为这git branch也是同样的事情。如果这两个命令完全不同,它们有何不同?

回答by Fatih Acet

git checkout -b BRANCH_NAMEcreates a new branch and checks out the new branch while git branch BRANCH_NAMEcreates a new branch but leaves you on the same branch.

git checkout -b BRANCH_NAME创建一个新分支并签出新分支,同时git branch BRANCH_NAME创建一个新分支但将您留在同一个分支上。

In other words git checkout -b BRANCH_NAMEdoes the following for you.

换句话说git checkout -b BRANCH_NAME,为您执行以下操作。

git branch BRANCH_NAME    # create a new branch
git checkout BRANCH_NAME  # then switch to the new branch

回答by manojlds

git branchcreates the branch but you remain in the current branch that you have checked out.

git branch创建分支,但您仍保留在已签出的当前分支中。

git checkout -bcreates a branch and checks it out.

git checkout -b创建一个分支并检查它。

It could be considered a short form of:

它可以被认为是以下的一种简短形式:

git branch name
git checkout name

回答by Michel Pereira

  • git branch:Shows all your branches
  • git branch newbranch:Creates a new branch
  • git checkout -b newbranch:Creates a new branch and switches to that branch immediately. This is the same as git branch newbranchfollowed by git checkout newbranch.
  • git branch:显示你所有的分支
  • git branch newbranch:创建一个新分支
  • git checkout -b newbranch创建一个新分支并立即切换到该分支。这与git branch newbranch后面的 相同git checkout newbranch

回答by Tuong Le

Full syntax:

完整语法:

git checkout -b [NEW_BRANCH] [FROM_BRANCH]

The [FROM_BRANCH] is optional. If there's no FROM_BRANCH, git will use the current branch.

[FROM_BRANCH] 是可选的。如果没有 FROM_BRANCH,git 将使用当前分支。

回答by ddavison

There is also another flag to mention, which is relative to these..

还有一个flag要提,就是相对于这些..

git checkout -B BRANCH_NAME

This is a very useful command that i've been using recently. This command checks out the branch you specify, and resetsthe branch based on the source branch.

这是我最近一直在使用的一个非常有用的命令。此命令检出您指定的分支,并根据源分支重置分支。

回答by Pshemy108

There are forms of both commands that are similar (looking at git-scm docs Version 2.11.1):

这两个命令的形式相似(查看 git-scm 文档版本 2.11.1):

git branch <branchname> <start-point>

and

git checkout -b <new_branch> <start_point>

The latterexecuting the branch command first and then adding the checkout. In that form explicitly references to git-branch's doc:

后者的第一执行分支命令,然后加入结帐。以这种形式明确引用 git-branch 的文档:

Specifying -b causes a new branch to be created as if git-branch[2] were called and then checked out

指定 -b 会导致创建一个新分支,就像调用了 git-branch[2] 然后检出一样

回答by user2238769

Essentially :

本质上 :

A-git branch lets you create a branch plain and simple.

A-git branch 可以让你创建一个简单明了的分支。

B -git checkout -b allows you to create a branch and switch to it at the same time.

B -git checkout -b 允许您创建一个分支并同时切换到它。

When will you use which ? 1- git branch when you want to create a branch but stay on the current branch. 2- git checkout -b when you want to create and switch. If you look at it is intuitive to create a branch and switch to it. So the choice is yours :)

你什么时候用哪个?1- git branch 当你想创建一个分支但留在当前分支上时。2- git checkout -b 当你想创建和切换时。如果你看一下,创建一个分支并切换到它是很直观的。所以选择是你的:)