内容简介:在上面是一个常见的例子,三个提交,这个时候我们需要重新检出
在 git
使用的时候,经常会碰到 DETACHED HEAD
,在此总结一下。
正文
HEAD normally refers to a named branch (e.g. master). Meanwhile, each branch refers to a specific commit. Let’s look at a repo with three commits, one of them tagged, and with branch master checked out:
HEAD (refers to branch 'master')
|
v
a---b---c branch 'master' (refers to commit 'c')
^
|
tag 'v2.0' (refers to commit 'b')
上面是一个常见的例子,三个提交, HEAD
指针指向 c
,往往是这个分支上最后的提交。然后又进行了一次修改和提交,生成了 d
。
$ edit; git add; git commit
HEAD (refers to branch 'master')
|
v
a---b---c---d branch 'master' (refers to commit 'd')
^
|
tag 'v2.0' (refers to commit 'b')
这个时候我们需要重新检出 v2.0
版本(这种可能性是很大,经常容易出现的),如下:
$ git checkout v2.0 # or $ git checkout master^^
HEAD (refers to commit 'b')
|
v
a---b---c---d branch 'master' (refers to commit 'd')
^
|
tag 'v2.0' (refers to commit 'b')
这个时候 HEAD
指针就指向了 b
,这就是 detached HEAD
状态,这意味着 HEAD
指向了某一个提交了,而不再指向当前分支的最后一个提交了。
然后我们又进行了一次提交,就会变成这样:
$ edit; git add; git commit
HEAD (refers to commit 'e')
|
v
e
/
a---b---c---d branch 'master' (refers to commit 'd')
^
|
tag 'v2.0' (refers to commit 'b')
one more time,再来一次:
$ edit; git add; git commit
HEAD (refers to commit 'f')
|
v
e---f
/
a---b---c---d branch 'master' (refers to commit 'd')
^
|
tag 'v2.0' (refers to commit 'b')
我们可以做任何正常的 git
操作,如果你想回到 master
分支,那么。
$ git checkout master
HEAD (refers to branch 'master')
e---f |
/ v
a---b---c---d branch 'master' (refers to commit 'd')
^
|
tag 'v2.0' (refers to commit 'b')
这个时候要意识到没有指针指向 f
提交,最后 e
和 f
都会被常规的Git垃圾回收所删除掉,除非我们创建一个指针,例如:
$ git checkout -b foo (1) $ git branch foo (2) $ git tag foo (3)
-
创建了一个新的分支指向
f,并且更新了HEAD指针,这样HEAD指针就不再是detached状态了 -
简单创建了一个新的分支指向
f,这个时候HEAD指针仍然是detached状态。
3.创建了一个新tag,指向f,这个时候HEAD指针仍然是detached状态。
If we have moved away from commit f, then we must first recover its object name (typically by using git reflog), and then we can create a reference to it. For example, to see the last two commits to which HEAD referred, we can use either of these commands:
如果 f
已经被移除了,我们首先需要恢复它的对象名,使用 git reflog
,然后我们创建一个指针指向它。例如,想看到 HEAD
之前的最后两个提交,我们可以使用下面的命令(二选一):
$ git reflog -2 HEAD # or $ git log -g -2 HEAD
总结
这篇总结基本上还是以翻译为主,留了一个问题,就是 reflog
。
以上所述就是小编给大家介绍的《Git:DETACHED HEAD的概念》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Creative Selection
Ken Kocienda / St. Martin's Press / 2018-9-4 / USD 28.99
Hundreds of millions of people use Apple products every day; several thousand work on Apple's campus in Cupertino, California; but only a handful sit at the drawing board. Creative Selection recounts ......一起来看看 《Creative Selection》 这本书的介绍吧!
JSON 在线解析
在线 JSON 格式化工具
RGB HSV 转换
RGB HSV 互转工具