`checkout -B` vs.`symbolic-ref` [英] `checkout -B` vs. `symbolic-ref`
问题描述
以下命令等效吗?如果没有,有什么区别?
Are the following commands equivalent? If not, what are the differences?
git checkout -B a_branch
和
git branch -f a_branch HEAD
git symbolic-ref HEAD refs/heads/a_branch
git branch -f a_branch HEAD
git symbolic-ref HEAD refs/heads/a_branch
另请参见此相关帖子.
推荐答案
是的,它们非常接近,以至于它们可能完全相同.
Yes, they are extremely close, so much so that they might as well be identical.
如果您稍微更改其中一个,它们的紧密度将大大降低.考虑您可以将其用作:
They become much less close if you change one of them slightly. Consider that you can use this as:
git checkout -B name commit-specifier
git checkout -B name commit-specifier
以及:
git checkout -B name
git checkout -B name
这实际上意味着:
git checkout -B name HEAD
git checkout -B name HEAD
如文档所述, -b
和-B
标志是某些替代命令序列的事务等效项".如果这些命令在执行过程中某处失败(或确实会失败),则-B
操作将被抑制.实际上,当您有未提交的更改会被结帐覆盖时,git checkout commit-specifier
实际上可能会失败.
As the documentation says, the -b
and -B
flags are the "transactional equivalent" of some alternative sequence of commands. If those commands would (or do) fail somewhere along the way, the -b
or -B
action is suppressed. And git checkout commit-specifier
can in fact fail, when you have uncommitted changes that would be overwritten by checkout.
但是,git checkout HEAD
应该永远都不会失败.鉴于它实际上不会失败,因此-B
操作的事务性变得不重要.现在,我们来看一下文档中所说的事务等效项:
However, git checkout HEAD
should never fail. Given that it won't actually fail, the transactional nature of the -B
operation becomes unimportant. So now we look at what the documentation says this is the transactional-equivalent-of:
$ git branch -f <branch> [<start point>]
$ git checkout <branch>
我们知道起点是HEAD
,所以:
We know that the start-point is HEAD
, so:
git branch -f a_branch HEAD
是正确的:这与第一个命令匹配.而且,我们知道git checkout <the commit we are already on>
本质上是无操作的(不会更改索引和工作树),而git checkout a_branch
最终会做:
is correct: this matches the first command. And, we know that git checkout <the commit we are already on>
is essentially a no-op (does not change the index and work-tree) and git checkout a_branch
ends up doing:
git symbolic-ref HEAD refs/heads/a_branch
作为其最终操作,因此:
as its final operation, so:
git checkout -B a_branch
手段":
- 不对索引和工作树做任何事情(成功);
- 如果成功(执行此操作),则将
a_branch
重置为当前提交;和 - 如果成功(确实如此),请使
HEAD
引用a_branch
.
- don't do anything to the index and work-tree (which succeeds);
- if that succeeds (which it does), reset
a_branch
to the current commit; and - if that succeeds (which it does), make
HEAD
refer toa_branch
.
但是,如果我们添加起点,则第1步可能会失败,而第2步(如果运行)可能会有所不同.
If we add the starting point, though, step 1 may fail, and step 2, if run, does something different.
这篇关于`checkout -B` vs.`symbolic-ref`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!