参考资料
本文讲述的 是 git 命令中的 HEAD ,适用于 github ,不适用于 gerrit
本文不对git进行介绍,只是收集一些和 HEAD 相关的链接
- 实例
$ git branch -a
dev
* master
remotes/origin/HEAD -> origin/master
remotes/origin/dev
remotes/origin/master
- 解读
1/第一个HEAD
从上面的示例可以看到有第三行有 一个 * 号.这个是一个HEAD 标识
2/第二个HEAD
第四行有一个 HEAD ,这个HEAD 是 remotes/origin/HEAD
-----------------
在文件中
1/第一个HEAD
$ cat .git/HEAD
ref: refs/heads/dev
2/第二个HEAD
$ cat .git/refs/remotes/origin/HEAD
ref: refs/remotes/origin/master
HEAD的实质,请参见
else
- 从上面可以看到 第一种HEAD 都是指向了分支,分支指向了分支上的最新提交
- 其实HEAD除了可以指向分支,也可以指向提交
- 具体请参见 2.5 检出之前的提交
git checkout <commit>
更新工作目录中的所有文件,使得和某个特定提交中的文件一致。
你可以将提交的哈希字串,或是标签作为 <commit> 参数。这会使你处在分离 HEAD 的状态。
当HEAD处于分离状态(不依附于任一分支)时,提交操作可以正常进行,但是不会更新任何已命名的分支。(你可以认为这是在更新一个匿名分支。)
$ git checkout 3e61d72da33650ecb5b6ac4b8395fde3c4fb950c
Note: checking out '3e61d72da33650ecb5b6ac4b8395fde3c4fb950c'.
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 3e61d72... 10
$ git status
HEAD detached at 3e61d72
nothing to commit, working directory clean
$ cat .git/HEAD
3e61d72da33650ecb5b6ac4b8395fde3c4fb950c
$ git checkout master
Previous HEAD position was 3e61d72... 10
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
- 综上: head 指向提交,也可以指向分支.
- 分支永远指向 分支上的最新提交
版权声明:本文为u011011827原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。