简体   繁体   English

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

[英]What is the difference between git branch and git checkout -b?

I'm very new to git/gitlab and want to confirm the difference between the 2 commands in the first screen shot.我对 git/gitlab 很陌生,想确认第一个屏幕截图中两个命令之间的区别。 What is the purpose of having 2 commands which both create new branches.拥有两个创建新分支的命令的目的是什么。 Why is 'git branch' even needed?为什么甚至需要'git branch'?

Is it purely there in case i want to create a new branch but not switch to that branch?如果我想创建一个新分支但不切换到该分支,它是否纯粹存在? So i very minor time save instead of doing 'git checkout -b new_branch' and then 'git checkout previous_branch'.所以我节省了很少的时间,而不是执行“git checkout -b new_branch”然后“git checkout previous_branch”。

Also why does it say to reference the current HEAD?另外为什么说要引用当前的 HEAD? I assume this is related to the 'remotes/origin/HEAD -> origin/master' in the second screen shot?我认为这与第二个屏幕截图中的“remotes/origin/HEAD -> origin/master”有关?

I tried 'git branch test_branch_name' which created the new branch without needed to reference the current HEAD, so why does it mention referencing the current HEAD?我尝试了“git branch test_branch_name”,它创建了新分支而不需要引用当前的 HEAD,那么为什么它提到引用当前的 HEAD?

在此处输入图像描述

在此处输入图像描述

Probably not necessary, but here's a link for where the first screen shot comes from: https://about.gitlab.com/images/press/git-cheat-sheet.pdf可能没有必要,但这里是第一个屏幕截图来源的链接: https://about.gitlab.com/images/press/git-cheat-sheet.Z437175BA4191210EE004E1D9347

To fill in a few more details:要填写更多详细信息:

... Why is 'git branch' even needed? ...为什么甚至需要'git branch'?

The git branch command has many options, such as: git branch命令有很多选项,例如:

git branch --set-upstream-to

or:或者:

git branch --list

or:或者:

git branch -d

which do things that neither git checkout nor the new (Git 2.23 or later) git switch will do. git checkout和新的(Git 2.23 或更高版本) git switch都不会做的事情。

The things that git branch --list does can be done with git for-each-ref . git branch --list所做的事情可以git for-each-ref来完成。 The thing that git branch --set-upstream-to does can be done with git config . git branch --set-upstream-to所做的事情可以通过git config来完成。 If you'd like an analogy, Git commands are tools .如果您想打个比方,Git 命令就是工具 You can cut wood with a bandsaw, a hacksaw, a coping saw, a drill, or a chisel.您可以使用带锯、钢锯、平顶锯、钻头或凿子切割木材。 If you'd like to make dovetails, most of these tools could do the job—but it will go a lot faster and better and easier with a router and a dovetailing jig.如果您想制作燕尾榫,这些工具中的大多数都可以完成这项工作,但使用路由器和燕尾夹具,它会更快、更好、更轻松。 If your goal is to put in a lock and doorknob set, consider a hole saw and the drill.如果您的目标是安装锁和门把手,请考虑使用孔锯和钻头。

(The git checkout command actually has too many tools built into it—in my opinion anyway. Apparently, finally, the official Git maintenance folks are coming around to this same opinion, as two of the main jobs that git checkout can perform are now provided by two separate front-end commands: git switch and git restore . Of course, in typical Git fashion, git restore can now do a few things that git checkout can't.) (在我看来, git checkout命令实际上内置了太多工具。显然,最后,官方的 Git 维护人员也提出了同样的观点,因为 ZBA9F11ECC 执行的两个主要工作现在可以提供git checkout by two separate front-end commands: git switch and git restore . Of course, in typical Git fashion, git restore can now do a few things that git checkout can't.)

Git is in some ways mainly a graph-manipulating suite of tools that happen to drag source code along with them. Git 在某些方面主要是一个图形处理工具套件,碰巧会拖拽源代码。 The graph you manipulate is the commit graph , which is made up of nodes (graph vertices) that are commits, connected by one-way edge links (arcs) that are the parent hash IDs stored in each commit-node.您操作的图是提交图,它由提交的节点(图顶点)组成,通过单向边链接(弧)连接,这些边链接是存储在每个提交节点中的父 hash ID。 See Think Like (a) Git .请参阅像 (a) Git 一样思考

git checkout -b [name] is short for git branch [name] && git checkout [name] . git checkout -b [name]git branch [name] && git checkout [name]的缩写。 The recent versions of Git also support git switch to checkout a branch.最新版本的 Git 还支持git switch以检查分支。

git branch [name] is equivalent to git branch [name] HEAD . git branch [name]等价于git branch [name] HEAD git branch [name] is a special case of git branch [name] [start-point] . git branch [name]git branch [name] [start-point]的特例。 When start-point is omitted, it defaults to HEAD .省略start-point时,默认为HEAD

For more details, see git branch .更多详细信息,请参见git 分支

暂无
暂无

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

相关问题 Git命令:git checkout -b和有什么不一样 <branch><remote branch> 和git分支 <branch><remote branch> ? - Git command: what is the difference between git checkout -b <branch> <remote branch> and git branch <branch> <remote branch>? “git branch”和“git checkout -b”有什么区别? - What is the difference between "git branch" and "git checkout -b"? git checkout -b branch和git checkout -b branch origin / branch有什么区别? - Whats the difference between `git checkout -b branch` and `git checkout -b branch origin/branch` git checkout --track origin/branch 和 git checkout -b branch origin/branch 之间的区别 - Difference between git checkout --track origin/branch and git checkout -b branch origin/branch 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结帐有什么区别<branch></branch> - What's the difference between git switch and git checkout <branch> git checkout和有什么不一样 <remote> / <branch> vs git checkout <branch> ? - What's the difference between git checkout <remote>/<branch> vs git checkout <branch>? git checkout remotes/origin/branch 和 git checkout branch 有什么区别? - What is difference between git checkout remotes/origin/branch and git checkout branch? repo start和git checkout -b之间的区别 - Difference between repo start and git checkout -b 在git中checkout远程分支和pull远程分支之间的区别? - Difference between checkout remote branch and pull remote branch in git?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM