如果让HEAD文件指向一个commit id,那就变成了detached HEAD。git checkout 可以达到这个效果,用下面的命令:
git checkout 1aea8d9^
laea8d9是最近的一次commit id,^指的是之前一次,因此上面的操作结果是让HEAD文件包含了倒数第二次提交的id.
下面演示如何进入datached HEAD状态,并恢复回来。
$ git branch -v
* master 89f8dae [ahead 2] update x
$ git checkout 89f8dae^
Note: checking out '89f8dae^'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b new_branch_name
HEAD is now at 1aea8d9... add test file x
好,现在恢复回来。
$ git checkout master
Previous HEAD position was 1aea8d9... add test file x
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 2 commits.
我并不清楚detached HEAD有何实际用处,反正就是一个让HEAD随便指向某个commit id,而不在乎是哪个branch的功能。
--------------------- 本文来自 csfreebird 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/csfreebird/article/details/7583363?utm_source=copy