##說明
本篇文章是對Pro Git 7.1 Git 工具 - 選擇修訂版本 章節中 祖先引用 的總結。
##git命令中HEAD^和HEAD~區別。
- HEAD+數字表示當前提交的父提交。具體是第幾個父提交共同過+數字指定,EAD^1第一個父提交,該語法只能用於合併(merge)的提交記錄,因爲一個通過合併產生的commit對象纔有多個父提交。
- HEAD~(等同於HEAD^,注意沒有加數字)表當前提交的上一個提交。
使用場景:如果想獲取一個提交的第幾個父提交使用HEAD+數字,想獲取一個提交的上幾個提交使用HEAD~。HEAD和HEAD或HEAD^^和HEAD並沒有區別,只有HEAD^+數字才和HEAD有區別。
我們現在有個一個master分支,我們通過在master分支執行這兩個命令了來了解一下他們之間的區別,當前master分支提交記錄如下:
Administrator@USER-20171018VX MINGW64 /d/branch-test (master)
$ git log --oneline --graph
* ae64286 (HEAD -> master) Merge branch 'iss11'
|\
| * 34af7c6 (iss11) 添加 run()
* | 247c954 添加 testRun()
|/
* 79779c7 添加 main()
* d44c096 Merge branch 'dev' 將dev分支內容合併到master分支
|\
| * 89a6a5a (dev) 介紹你可以向文件寫入什麼內容
* | bd61a96 添加介紹說明
|/
* 01833f9 添加文件類型說明
* 5f19dbf 新建文件a.txt和b.txt
##1,HEAD^
###1.1,最新一次提交的第一個父提交。
Administrator@USER-20171018VX MINGW64 /d/branch-test (master)
$ git show HEAD^1
commit 247c954617cdcb457ced546bb7244ce61b3ccc2b
Author: zhangxy <[email protected]>
Date: 2018-08-30 10:33:26 +0800
添加 testRun()
diff --git a/b.txt b/b.txt
index 3473edf..5fe14d5 100644
--- a/b.txt
+++ b/b.txt
@@ -1 +1,4 @@
my name is b file
+public void testRun(){
+
+}
###1.2,最新一次提交的第二個父提交
Administrator@USER-20171018VX MINGW64 /d/branch-test (master)
$ git show HEAD^2
commit 34af7c6b386f5e09d24340e0f8a1b1f6b5bfd47d (iss11)
Author: zhangxy <[email protected]>
Date: 2018-08-30 10:30:19 +0800
添加 run()
diff --git a/a.txt b/a.txt
index d196d14..50e3bfc 100644
--- a/a.txt
+++ b/a.txt
@@ -5,3 +5,8 @@ my name is file a
public static void main(String [] args){
}
+
+public void run(){
+
+}
###1.3,最新一次提交的上兩個提交。
Administrator@USER-20171018VX MINGW64 /d/branch-test (master)
$ git show HEAD^^
commit 79779c75aeea6a4f514d1c77db5d86431011e117
Author: zhangxy <[email protected]>
Date: 2018-08-30 10:26:15 +0800
添加 main()
diff --git a/a.txt b/a.txt
index 759ac2f..d196d14 100644
--- a/a.txt
+++ b/a.txt
@@ -2,3 +2,6 @@ my name is file a
我是一個txt類型文件
你可以輸入漢子並不會出現亂碼
你可以向這裏寫入字符
+public static void main(String [] args){
+
+}
##2,HEAD~
###2.1,最新一次提交的上兩個提交。
Administrator@USER-20171018VX MINGW64 /d/branch-test (master)
$ git show HEAD~2
commit 79779c75aeea6a4f514d1c77db5d86431011e117
Author: zhangxy <[email protected]>
Date: 2018-08-30 10:26:15 +0800
添加 main()
diff --git a/a.txt b/a.txt
index 759ac2f..d196d14 100644
--- a/a.txt
+++ b/a.txt
@@ -2,3 +2,6 @@ my name is file a
我是一個txt類型文件
你可以輸入漢子並不會出現亂碼
你可以向這裏寫入字符
+public static void main(String [] args){
+
+}
##3,HEAD^和HEAD~結合使用
###3.1,想獲取89a6a5a提交對象。
Administrator@USER-20171018VX MINGW64 /d/branch-test (master)
$ git show HEAD~3^2
commit 89a6a5a6f544ff74cc49341aab1484b084be7759 (dev)
Author: zhangxy <[email protected]>
Date: 2018-08-29 21:23:50 +0800
介紹你可以向文件寫入什麼內容
diff --git a/a.txt b/a.txt
index e5f555c..0cde68b 100644
--- a/a.txt
+++ b/a.txt
@@ -1,2 +1,3 @@
my name is file a
我是一個txt類型文件
+你可以向這裏寫入字符