1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
pande@raspberrypi:~ $ mkdir git_time_machine
pande@raspberrypi:~ $ cd git_time_machine
pande@raspberrypi:~/git_time_machine $ git init
提示:將「master」設定為初始分支的名稱。這個預設分支名稱可以變更。
提示:如果要設定所有新版本庫要使用的初始分支名稱,
提示:請呼叫(會隱藏這個警告):
提示:
提示: git config —global init.defaultBranch <name>
提示:
提示:除了 ‘master’ 外,常用的分支名稱有 ‘main’, ‘trunk’ 以及
提示:‘development’。剛建立的分支可以用這個命令重新命名:
提示:
提示: git branch -m <name>
pande@raspberrypi:~/git_time_machine $ echo "This is the first line of my document." > document.txt
pande@raspberrypi:~/git_time_machine $ git add document.txt
pande@raspberrypi:~/git_time_machine $ git commit -m "Initial commit: Add document.txt"
作者身分未知

*** 請告訴我你是誰。

執行

  git config --global user.email "[email protected]"
  git config --global user.name "Your Name"

來設定您帳號的預設身份標記。
如果僅在本版本庫設定身份標記,則省略 --global 參數。
fatal: 無法自動偵測信件位址(得到 'pande@raspberrypi.(none)')
→→→ 這個我知道,之前也有遇到,做過練習。
pande@raspberrypi:~/git_time_machine $ git config user.email "[email protected]"
pande@raspberrypi:~/git_time_machine $ git config user.name "Pande"
pande@raspberrypi:~/git_time_machine $ git commit -m "Initial commit: Add document.txt"
[master (根提交) dff366f] Initial commit: Add document.txt
 1 file changed, 1 insertion(+)
 create mode 100644 document.txt
pande@raspberrypi:~/git_time_machine $ git status
位於分支 master
沒有要提交的檔案,工作區為乾淨狀態
pande@raspberrypi:~/git_time_machine $ git log
commit dff366f0bb3278629d70356a44e7eb0956430c5d (HEAD -> master)
Author: Pande <[email protected]>
Date:   Sun Jul 20 01:24:00 2025 +0800

    Initial commit: Add document.txt
pande@raspberrypi:~/git_time_machine $ echo "This is the second line, added later." >> document.txt
pande@raspberrypi:~/git_time_machine $ git add document.txt
pande@raspberrypi:~/git_time_machine $ git commit -m "Add second line"
[master 78bd6c6] Add second line
 1 file changed, 1 insertion(+)
pande@raspberrypi:~/git_time_machine $ echo "This is the final line, concluding the document." >> document.txt
pande@raspberrypi:~/git_time_machine $ git add document.txt
pande@raspberrypi:~/git_time_machine $ git commit -m "Add final line"
[master ee8c3a4] Add final line
 1 file changed, 1 insertion(+)
pande@raspberrypi:~/git_time_machine $ git log
commit ee8c3a42a0dd76e0fe5230b57a84d71b9b653cf4 (HEAD -> master)
Author: Pande <[email protected]>
Date:   Sun Jul 20 01:26:00 2025 +0800

    Add final line

commit 78bd6c63bf79fcd646cba87a960e050e021f2477
Author: Pande <[email protected]>
Date:   Sun Jul 20 01:25:24 2025 +0800

    Add second line

commit dff366f0bb3278629d70356a44e7eb0956430c5d
Author: Pande <[email protected]>
Date:   Sun Jul 20 01:24:00 2025 +0800

    Initial commit: Add document.txt
→→→ 「練習專案」準備完成
pande@raspberrypi:~/git_time_machine $ git diff
→→→ 還沒有新變化
pande@raspberrypi:~/git_time_machine $ echo "A new line that is not yet committed." >> document.txt
pande@raspberrypi:~/git_time_machine $ git diff
diff --git a/document.txt b/document.txt
index 3057a06..67b0578 100644
--- a/document.txt
+++ b/document.txt
@@ -1,3 +1,4 @@
 This is the first line of my document.
 This is the second line, added later.
 This is the final line, concluding the document.
+A new line that is not yet committed.
→→→ 喔喔出現了!還沒加入的變化
→→→ 請問這些資料怎麼解讀?
pande@raspberrypi:~/git_time_machine $ git diff --staged
→→→ 還沒已加入的變化
pande@raspberrypi:~/git_time_machine $ git diff --cached
→→→ cached跟staged一樣
pande@raspberrypi:~/git_time_machine $ git add document.txt
pande@raspberrypi:~/git_time_machine $ git diff --staged
diff --git a/document.txt b/document.txt
index 3057a06..67b0578 100644
--- a/document.txt
+++ b/document.txt
@@ -1,3 +1,4 @@
 This is the first line of my document.
 This is the second line, added later.
 This is the final line, concluding the document.
+A new line that is not yet committed.
→→→ 已加入,尚未提交的變化
pande@raspberrypi:~/git_time_machine $ git diff --cached
diff --git a/document.txt b/document.txt
index 3057a06..67b0578 100644
--- a/document.txt
+++ b/document.txt
@@ -1,3 +1,4 @@
 This is the first line of my document.
 This is the second line, added later.
 This is the final line, concluding the document.
+A new line that is not yet committed.
→→→ cached跟staged完全一樣?
pande@raspberrypi:~/git_time_machine $ git diff HEAD HEAD~1
diff --git a/document.txt b/document.txt
index 3057a06..292c15c 100644
--- a/document.txt
+++ b/document.txt
@@ -1,3 +1,2 @@
 This is the first line of my document.
 This is the second line, added later.
-This is the final line, concluding the document.
pande@raspberrypi:~/git_time_machine $ git diff HEAD~2 HEAD
diff --git a/document.txt b/document.txt
index 2296315..3057a06 100644
--- a/document.txt
+++ b/document.txt
@@ -1 +1,3 @@
 This is the first line of my document.
+This is the second line, added later.
+This is the final line, concluding the document.
pande@raspberrypi:~/git_time_machine $ git diff HEAD~2 HEAD~1
diff --git a/document.txt b/document.txt
index 2296315..292c15c 100644
--- a/document.txt
+++ b/document.txt
@@ -1 +1,2 @@
 This is the first line of my document.
+This is the second line, added later.
→→→ 各種「比較版本變化」
pande@raspberrypi:~/git_time_machine $ git diff HEAD~3 master
fatal: 有歧義的參數 'HEAD~3':未知的版本或路徑不存在於工作區中。
使用 '--' 來分隔版本和路徑,例如:
'git <指令> [<版本>...] -- [<檔案>...]'
→→→ 喔喔,過頭了😶‍🌫️
pande@raspberrypi:~/git_time_machine $ git diff HEAD~2 master
diff --git a/document.txt b/document.txt
index 2296315..3057a06 100644
--- a/document.txt
+++ b/document.txt
@@ -1 +1,3 @@
 This is the first line of my document.
+This is the second line, added later.
+This is the final line, concluding the document.
→→→ 跟master比較變化
pande@raspberrypi:~/git_time_machine $ git diff HEAD~1
diff --git a/document.txt b/document.txt
index 292c15c..67b0578 100644
--- a/document.txt
+++ b/document.txt
@@ -1,2 +1,4 @@
 This is the first line of my document.
 This is the second line, added later.
+This is the final line, concluding the document.
+A new line that is not yet committed.
→→→ 某版本跟最新 比較變化
pande@raspberrypi:~/git_time_machine $ git diff dff366f HEAD
diff --git a/document.txt b/document.txt
index 2296315..3057a06 100644
--- a/document.txt
+++ b/document.txt
@@ -1 +1,3 @@
 This is the first line of my document.
+This is the second line, added later.
+This is the final line, concluding the document.
→→→ 指定SHA-1版本 跟最新比較變化
pande@raspberrypi:~/git_time_machine $ git diff dff366f HEAD document.txt
diff --git a/document.txt b/document.txt
index 2296315..3057a06 100644
--- a/document.txt
+++ b/document.txt
@@ -1 +1,3 @@
 This is the first line of my document.
+This is the second line, added later.
+This is the final line, concluding the document.
→→→ 針對特定檔案 指定SHA-1版本 跟最新比較變化
pande@raspberrypi:~/git_time_machine $ git show dff366f:document.txt 
This is the first line of my document.
→→→ 指定SHA-1版本 顯示特定檔案內容
pande@raspberrypi:~/git_time_machine $ git switch --detach dff366f
error: 您對下列檔案的本機修改將被檢出動作覆蓋:
document.txt
請在切換分支前提交或儲藏您的修改。
正在終止
→→→ 要我先把已有變化做提交… 🤔
pande@raspberrypi:~/git_time_machine $ git commit -m 'commit for switch practice'
[master 091c388] commit for switch practice
 1 file changed, 1 insertion(+)
pande@raspberrypi:~/git_time_machine $ git switch --detach dff366f
HEAD 目前位於 dff366f Initial commit: Add document.txt
→→→ 切換至特定SHA-1版本
pande@raspberrypi:~/git_time_machine $ git log
commit dff366f0bb3278629d70356a44e7eb0956430c5d (HEAD)
Author: Pande <[email protected]>
Date:   Sun Jul 20 01:24:00 2025 +0800

    Initial commit: Add document.txt
→→→ 喔喔,這麼清爽😯
→→→ 我有去查看檔案,內容真的被git修改(回去)了
pande@raspberrypi:~/git_time_machine $ git switch master
之前的 HEAD 位置是 dff366f Initial commit: Add document.txt
切換到分支 'master'
pande@raspberrypi:~/git_time_machine $ git log
commit 091c388028a08d03ac009f8df06ddb4a7c89d07c (HEAD -> master)
Author: Pande <[email protected]>
Date:   Sun Jul 20 01:48:08 2025 +0800

    commit for switch practice

commit ee8c3a42a0dd76e0fe5230b57a84d71b9b653cf4
Author: Pande <[email protected]>
Date:   Sun Jul 20 01:26:00 2025 +0800

    Add final line

commit 78bd6c63bf79fcd646cba87a960e050e021f2477
Author: Pande <[email protected]>
Date:   Sun Jul 20 01:25:24 2025 +0800

    Add second line

commit dff366f0bb3278629d70356a44e7eb0956430c5d
Author: Pande <[email protected]>
Date:   Sun Jul 20 01:24:00 2025 +0800

    Initial commit: Add document.txt
→→→ 切換回master版本 & 確認檔案裡面內容恢復
pande@raspberrypi:~/git_time_machine $ git reset --hard dff366f
HEAD 現在位於 dff366f Initial commit: Add document.txt
→→→ 重設至特定SHA-1版本
pande@raspberrypi:~/git_time_machine $ git log
commit dff366f0bb3278629d70356a44e7eb0956430c5d (HEAD -> master)
Author: Pande <[email protected]>
Date:   Sun Jul 20 01:24:00 2025 +0800

    Initial commit: Add document.txt
→→→ 乍看之下跟switch有像,不過一個是(HEAD)一個是(HEAD -> master)
pande@raspberrypi:~/git_time_machine $ git reflog
dff366f (HEAD -> master) HEAD@{0}: reset: moving to dff366f
091c388 HEAD@{1}: checkout: moving from dff366f0bb3278629d70356a44e7eb0956430c5d to master
dff366f (HEAD -> master) HEAD@{2}: checkout: moving from master to dff366f
091c388 HEAD@{3}: commit: commit for switch practice
ee8c3a4 HEAD@{4}: commit: Add final line
78bd6c6 HEAD@{5}: commit: Add second line
dff366f (HEAD -> master) HEAD@{6}: commit (initial): Initial commit: Add document.txt
→→→ 我有另外查詢,reflog相對於log,是reference log的意思
→→→ HEAD@{1}怎麼解讀?{}裡面數值表示跟現下的版本差異次數
→→→ commit產生版本;switch發生checkout: moving;reset發生reset: moving
pande@raspberrypi:~/git_time_machine $ git reset --hard 091c388
HEAD 現在位於 091c388 commit for switch practice
→→→ 練習完成!👏