HEAD^+数字
:表示当前提交的父提交,具体是第几个父提交通过+数字指定,HEAD^1第一个父提交,该语法只
能用于合并(merge)的提交记录,因为一个通过合并产生的commit对象才有多个父提交。
HEAD~+数字
:(等同于HEAD^,注意没有加数字)。表示当前提交的上一个提交,具体是第几个提交通过+数字指
定,HEAD~1第一个提交。
HEAD^主要是控制merge之后回退的方向。
HEAD~主要是回退的步数。
# master分支
echo a > a.txt
git add a.txt
git commit -m "add a.txt"
echo b > b.txt
git add b.txt
git commit -m "add b.txt"
echo c > c.txt
git add c.txt
git commit -m "add c.txt"
$ git log --oneline
0cf861c (HEAD -> master) add c.txt
ca0bb41 add b.txt
4976001 add a.txt
# branch1分支
git checkout -b branch1
echo a1 > a.txt
git add a.txt
git commit -m "update a.txt"
echo b1 > b.txt
git add b.txt
git commit -m "update b.txt"
echo c1 > c.txt
git add c.txt
git commit -m "update c.txt"
$ git log --oneline
d1cf0f9 (HEAD -> branch1) update c.txt
5def268 update b.txt
007a512 update a.txt
0cf861c (master) add c.txt
ca0bb41 add b.txt
4976001 add a.txt
# branch2分支
git checkout master
git checkout -b branch2
echo d > d.txt
git add d.txt
git commit -m "add d.txt"
echo e > e.txt
git add e.txt
git commit -m "add e.txt"
echo f > f.txt
git add f.txt
git commit -m "add f.txt"
$ git log --oneline
424a045 (HEAD -> branch2) add f.txt
2601bd8 add e.txt
092224c add d.txt
0cf861c (master) add c.txt
ca0bb41 add b.txt
4976001 add a.txt
# branch3分支
git checkout master
git checkout -b branch3
echo g > g.txt
git add g.txt
git commit -m "add g.txt"
echo h > h.txt
git add h.txt
git commit -m "add h.txt"
echo i > i.txt
git add i.txt
git commit -m "add i.txt"
$ git log --oneline
af44be3 (HEAD -> branch3) add i.txt
927481e add h.txt
f2339af add g.txt
0cf861c (master) add c.txt
ca0bb41 add b.txt
4976001 add a.txt
# 合并
git checkout master
$ git merge --no-ff branch1
Merge made by the 'recursive' strategy.
a.txt | 2 +-
b.txt | 2 +-
c.txt | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)
$ git merge --no-ff branch2
Merge made by the 'recursive' strategy.
d.txt | 1 +
e.txt | 1 +
f.txt | 1 +
3 files changed, 3 insertions(+)
create mode 100644 d.txt
create mode 100644 e.txt
create mode 100644 f.txt
$ git merge --no-ff branch3
Merge made by the 'recursive' strategy.
g.txt | 1 +
h.txt | 1 +
i.txt | 1 +
3 files changed, 3 insertions(+)
create mode 100644 g.txt
create mode 100644 h.txt
create mode 100644 i.txt
$ git log --oneline --graph
* 462cb43 (HEAD -> master) Merge branch 'branch3'
|\
| * af44be3 (branch3) add i.txt
| * 927481e add h.txt
| * f2339af add g.txt
* | 286ea08 Merge branch 'branch2'
|\ \
| * | 424a045 (branch2) add f.txt
| * | 2601bd8 add e.txt
| * | 092224c add d.txt
| |/
* | 0eede92 Merge branch 'branch1'
|\ \
| |/
|/|
| * d1cf0f9 (branch1) update c.txt
| * 5def268 update b.txt
| * 007a512 update a.txt
|/
* 0cf861c add c.txt
* ca0bb41 add b.txt
* 4976001 add a.txt
# 查看当前最新commit的信息
$ git show HEAD
commit 462cb43cdc468a24a90aecaff394ede36b2b0c37 (HEAD -> master)
Merge: 286ea08 af44be3
Author: zhangshixing <[email protected]>
Date: Fri May 26 16:18:34 2023 +0800
Merge branch 'branch3'
# 显示最新一次提交的第一个父提交
$ git show HEAD^1
commit 286ea083818c1e261e4ce50aaf88f3961bea2e36
Merge: 0eede92 424a045
Author: zhangshixing <[email protected]>
Date: Fri May 26 16:18:30 2023 +0800
Merge branch 'branch2'
# 显示最新一次提交的第二个父提交
$ git show HEAD^2
commit af44be3047507c4519bd7a52dc5c230b94c16338 (branch3)
Author: zhangshixing <[email protected]>
Date: Fri May 26 16:17:59 2023 +0800
add i.txt
diff --git a/i.txt b/i.txt
new file mode 100644
index 0000000..0ddf2ba
--- /dev/null
+++ b/i.txt
@@ -0,0 +1 @@
+i
# 显示最新一次提交的第三个父提交
# 报错说明没有
$ git show HEAD^3
fatal: ambiguous argument 'HEAD^3': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git [...] -- [...]'
# 查看当前最新commit的信息
$ git show HEAD
# 等价于
$ git show HEAD@{0}
commit 462cb43cdc468a24a90aecaff394ede36b2b0c37 (HEAD -> master)
Merge: 286ea08 af44be3
Author: zhangshixing <[email protected]>
Date: Fri May 26 16:18:34 2023 +0800
Merge branch 'branch3'
# 查看上一个提交
$ git show HEAD^
# 等价于
$ git show HEAD@{1}
commit 286ea083818c1e261e4ce50aaf88f3961bea2e36
Merge: 0eede92 424a045
Author: zhangshixing <[email protected]>
Date: Fri May 26 16:18:30 2023 +0800
Merge branch 'branch2'
# 查看上两个提交
$ git show HEAD^^
# 等价于
$ git show HEAD@{2}
commit 0eede928ea9d8493c9382e6fa6e27844fcd04db8
Merge: 0cf861c d1cf0f9
Author: zhangshixing <[email protected]>
Date: Fri May 26 16:18:27 2023 +0800
Merge branch 'branch1'
# 查看上三个提交
$ git show HEAD^^^
# 等价于
$ git show HEAD@{3}
commit 0cf861c0d10fc1b44c8807b12be23d23f28ce9f6
Author: zhangshixing <[email protected]>
Date: Fri May 26 16:16:40 2023 +0800
add c.txt
diff --git a/c.txt b/c.txt
new file mode 100644
index 0000000..f2ad6c7
--- /dev/null
+++ b/c.txt
@@ -0,0 +1 @@
+c
# 查看上四个提交
$ git show HEAD^^^^
# 等价于
$ git show HEAD@{4
commit ca0bb41c15c07326a228d428c78ed9f4ad86d27c
Author: zhangshixing <[email protected]>
Date: Fri May 26 16:16:39 2023 +0800
add b.txt
diff --git a/b.txt b/b.txt
new file mode 100644
index 0000000..6178079
--- /dev/null
+++ b/b.txt
@@ -0,0 +1 @@
+b
# HEAD^和HEAD~结合使用
# 第3个提交的第一个父提交
$ git show HEAD~3^1
commit ca0bb41c15c07326a228d428c78ed9f4ad86d27c
Author: zhangshixing <[email protected]>
Date: Fri May 26 16:16:39 2023 +0800
add b.txt
diff --git a/b.txt b/b.txt
new file mode 100644
index 0000000..6178079
--- /dev/null
+++ b/b.txt
@@ -0,0 +1 @@
+b
$ git reflog --oneline
462cb43 (HEAD -> master) HEAD@{0}: merge branch3: Merge made by the 'recursive' strategy.
286ea08 HEAD@{1}: merge branch2: Merge made by the 'recursive' strategy.
0eede92 HEAD@{2}: merge branch1: Merge made by the 'recursive' strategy.
0cf861c HEAD@{3}: checkout: moving from branch3 to master
af44be3 (branch3) HEAD@{4}: commit: add i.txt
927481e HEAD@{5}: commit: add h.txt
f2339af HEAD@{6}: commit: add g.txt
0cf861c HEAD@{7}: checkout: moving from master to branch3
0cf861c HEAD@{8}: checkout: moving from branch2 to master
424a045 (branch2) HEAD@{9}: commit: add f.txt
2601bd8 HEAD@{10}: commit: add e.txt
092224c HEAD@{11}: commit: add d.txt
0cf861c HEAD@{12}: checkout: moving from master to branch2
0cf861c HEAD@{13}: checkout: moving from branch1 to master
d1cf0f9 (branch1) HEAD@{14}: commit: update c.txt
5def268 HEAD@{15}: commit: update b.txt
007a512 HEAD@{16}: commit: update a.txt
0cf861c HEAD@{17}: checkout: moving from master to branch1
0cf861c HEAD@{18}: commit: add c.txt
ca0bb41 HEAD@{19}: commit: add b.txt
4976001 HEAD@{20}: commit (initial): add a.txt
$ git show HEAD@{2}
commit 0eede928ea9d8493c9382e6fa6e27844fcd04db8
Merge: 0cf861c d1cf0f9
Author: zhangshixing <[email protected]>
Date: Fri May 26 16:18:27 2023 +0800
Merge branch 'branch1'