摘录自本人《Git聊天入门》的章节: HEAD^ 和 HEAD~ 是啥
^
和~
是2个很有意思的字符,配合使用可表示祖宗十八代。任你给一个节点(HEAD 或 哈希值),都能顺藤摸瓜,找到其祖先是谁。
网友通常这么解释:
-
^
:表示第几个父/母亲 —— git存在多个分支合并的情况,所以不只有1对父母亲 -
~
:表示向上找第几代,相当于连续几个^
比如有这样一个库:
b1(HEAD)
|
C4 ------ C6
/
--- C5
自己: C6 = HEAD^0 = HEAD~0 = C6^0 = C6~0
父亲: C4 = HEAD^1 = HEAD^ = HEAD~1 = HEAD~ = C6^1 = C6^ = C6~1 = C6~
母亲: C5 = HEAD^2 = C6^2
我经过反复琢磨,给出另外一种更形象的解释:
-
^x
:抬头走1步,入x号岔路口。 -
~y
:低头走y步,无视岔路口。
所以:^^
: 抬头2步,都是1号口;^2^
:抬头走2步,第1步入2号口;^^3^
:抬头走3步,第2步入3号口;~^~
:低头1步,抬头1步,再低头1步;~2^~^2
:低头走2步,抬头1步如1号口,再低头1步,再抬头1步如2号口 …… 怎么样?用抬头走、低头走理论是不是一下就理解了?心情舒畅吧。
这样,我们就可以方便的得到:
爷爷:HEAD^^ = HEAD^~ = HEAD~2
奶奶:HEAD^^2
姥爷:HEAD^2^ = HEAD^2~
姥姥:HEAD^2^2
……
^
和 ~
可用于git的多种操作:log、diff、show、checkout……
实际操作一下:
kevin@:linux.git$ git log --oneline -n20 --graph
* c11fb13a117e (HEAD -> master, tuna/master, kernel/master, github/master) Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid
|\
| * 3ed224e273ac HID: logitech-dj: Fix 064d:c52f receiver support
| * f9482dabfd16 Revert "HID: core: Call request_module before doing device_add"
| * e0b7f9bc0246 Revert "HID: core: Do not call request_module() in async context"
| * 15fc1b5c8612 Revert "HID: Increase maximum report size allowed by hid_field_extract()"
| * eb6964fa6509 HID: i2c-hid: add iBall Aer3 to descriptor override
* | b076173a309e Merge tag 'selinux-pr-20190612' of git://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/selinux
|\ \
| * | fec6375320c6 selinux: fix a missing-check bug in selinux_sb_eat_lsm_opts()
| * | e2e0e09758a6 | 2019-06-12 21:28:21 +0800 | Gen Zhang selinux: fix a missing-check bug in selinux_add_mnt_opt( )
| * | aff7ed485168 | 2019-06-11 10:07:19 +0200 | Ondrej Mosnacek selinux: log raw contexts as untrusted strings
* | | 35110e38e6c5 | 2019-06-12 05:57:05 -1000 | Linus Torvalds Merge tag 'media/v5.2-2' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab/linux-media
自己:
kevin@:linux.git$ git log --oneline -n1 HEAD^0
c11fb13a117e (HEAD -> master, tuna/master, kernel/master, github/master) Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid
kevin@:linux.git$ git log --oneline -n1 HEAD~0
c11fb13a117e (HEAD -> master, tuna/master, kernel/master, github/master) Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid
父亲:
kevin@:linux.git$ git log --oneline -n1 HEAD^
b076173a309e Merge tag 'selinux-pr-20190612' of git://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/selinux
kevin@:linux.git$ git log --oneline -n1 HEAD^1
b076173a309e Merge tag 'selinux-pr-20190612' of git://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/selinux
kevin@:linux.git$ git log --oneline -n1 HEAD~
b076173a309e Merge tag 'selinux-pr-20190612' of git://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/selinux
kevin@:linux.git$ git log --oneline -n1 HEAD~1
b076173a309e Merge tag 'selinux-pr-20190612' of git://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/selinux
爷爷:
kevin@:linux.git$ git log --oneline -n1 HEAD^^
35110e38e6c5 Merge tag 'media/v5.2-2' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab/linux-media
kevin@:linux.git$ git log --oneline -n1 HEAD^~
35110e38e6c5 Merge tag 'media/v5.2-2' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab/linux-media
kevin@:linux.git$ git log --oneline -n1 HEAD~2
35110e38e6c5 Merge tag 'media/v5.2-2' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab/linux-media
奶奶
kevin@:linux.git$ git log --oneline -n1 HEAD^^2
fec6375320c6 selinux: fix a missing-check bug in selinux_sb_eat_lsm_opts()
母亲:
kevin@:linux.git$ git log --oneline -n1 HEAD^2
3ed224e273ac HID: logitech-dj: Fix 064d:c52f receiver support
姥爷:
kevin@:linux.git$ git log --oneline -n1 HEAD^2~
f9482dabfd16 Revert "HID: core: Call request_module before doing device_add"
总体来说:找父亲一族要方便些,找母亲一族要麻烦些。
网友评论