微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Learn Git Lesson06 - 分离头指针

==============

知识点

  • 分离头指针
  • HEAD 含义
  • git diff

分离头指针 (Detached HEAD)

有时候想尝试性修改某些内容(实验),也许并不会真的提交到分支,这时候可以使用分离头指针,修改内容不会进入任何分支。

修改内容的头指针没有与任何branch挂钩时,如果这时将头指针指向了某个分支(master),刚刚做的修改会被git当作垃圾废弃。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ max @ Maxs-MBP-2016 in ~/Develop/learnhtml on git:master o
$ git checkout 5009f3a5f4
Note: checking out '5009f3a5f4'.

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>
大专栏  Learn Git Lesson06 - 分离头指针
HEAD is Now at 5009f3a Add index.html

修改README 文件添加一行即可)

1
2
3
4
5
6
$ max @ Maxs-MBP-2016 in ~/Develop/learnhtml on git:5009f3a o 
$ ls
README.md index.html

$ max @ Maxs-MBP-2016 in ~/Develop/learnhtml on git:5009f3a o
$ vim README.md

如果这时再将HEAD指向master,刚才做的修改会被废弃(可以进入gitk –all 查看)

1
2
$ max @ Maxs-MBP-2016 in ~/Develop/learnhtml on git:5009f3a x
$ git checkout master

HEAD归根到底其实还是指向了一个commit,并且可以指代那个commit

git diff

git diff 用来比较两次commit的不同
可以使用HEAD指代当前分支最后一次commit或者
HEAD~1 , HEAD~2 分别表示倒数一次,倒数二次

1
2
3
4
$ max @ Maxs-MBP-2016 in ~/Develop/learnhtml on git:master x 
$ Git diff a8a599a4dd07 0434ef25597
$ Git diff HEAD HEAD~1
$ Git diff HEAD HEAD~2

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐