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

发布于 2024-12-13 06:51:52 字数 78 浏览 6 评论 0原文

我使用 git checkout -b 创建一个新分支。我认为 gitbranch 做了同样的事情。 如果这两个命令有不同的话,它们有何不同?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

故笙诉离歌 2024-12-20 06:51:52

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

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

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

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

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

git branch BRANCH_NAME    # create a new branch
git switch BRANCH_NAME    # then switch to the new branch
小霸王臭丫头 2024-12-20 06:51:52

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

git checkout -b 创建一个分支并将其签出。

它可以被认为是以下形式的缩写:

git branch name
git checkout name

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

git checkout -b creates a branch and checks it out.

It could be considered a short form of:

git branch name
git checkout name
物价感观 2024-12-20 06:51:52

完整语法:

git checkout -b [NEW_BRANCH] [FROM_BRANCH]

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

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.

◇流星雨 2024-12-20 06:51:52
  • gitbranch 显示所有分支
  • gitbranchnewbranch 创建新分支
  • git checkout -b newbranch 创建一个新分支并立即切换到该分支。这与 git Branch newbranch 后跟 git checkout newbranch 相同。
  • 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 newbranch followed by git checkout newbranch.
伴我心暖 2024-12-20 06:51:52

还有另一个标志需要提及,它与这些相关。

git checkout -B BRANCH_NAME

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

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

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 resets the branch based on the source branch.

岁月染过的梦 2024-12-20 06:51:52

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

git branch <branchname> <start-point>

以及

git checkout -b <new_branch> <start_point>

后者 先执行分支命令,然后添加结账。以该形式明确引用 git-branch 的文档:

指定 -b 会导致创建一个新分支,就像 git-branch[2] 一样
被打电话然后退房

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

git branch <branchname> <start-point>

and

git checkout -b <new_branch> <start_point>

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

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

递刀给你 2024-12-20 06:51:52

本质上:

A-git 分支让您可以简单明了地创建分支。

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

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

Essentially :

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

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

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文