-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_LabSteps_Git.txt
More file actions
322 lines (170 loc) · 7.53 KB
/
_LabSteps_Git.txt
File metadata and controls
322 lines (170 loc) · 7.53 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# Git 入門練習
1. 檢視與設定 Git 組態
1.1 在 Cloud9 工作環境,功能表 Window | New Terminal 新增一個終端機視窗
1.2 輸入指令: git config --list
<Note> 請特別觀察 user.name、user.email、core.editor 這三項設定值
1.3 輸入下列指令:
git config --global user.name "yourName"
git config --global user.email "you@email.address"
例如:
git config --global user.name "Chien"
git config --global user.email "stdnt25@gmail.com"
1.4 再次輸入指令: git config --list 觀察新的設定值
2. 建立 Repository (數據庫)
2.1 輸入下列指令,建立並切換到新的 gitLab 資料夾:
mkdir gitLab
cd gitLab
<Note> gitLab 是本次 Git 練習的工作目錄
2.2 在 Cloud9 工作環境左側的 Workspace,
以滑鼠右鍵點按新的 gitLab 資料夾 | New File 建立新檔案,
檔名: shopping.txt。檔案內容如下:
# Shpping List
- milk
- eggs
<Note> 記得要存檔喔! (Hint: File | Save)
2.3 在終端機視窗,輸入指令: git status 以了解工作目錄的現況
<Note> 出現錯誤訊息,指出在目前目錄找不到 git repository
fatal: Not a git repository (or any of the parent directories): .git
2.4 建立 Repository (數據庫),指令: git init
<Note> 觀察 gitLab 資料夾多出一個 .git 子資料夾
<Note> 螢幕左上方的「齒輪」圖示,有開關可顯示系統隱藏資料夾。
2.5 再次使用 git status 了解工作目錄的現況:
<Note> Git 系統告訴我們:
shopping.txt 不在 Git 管制之列,
建議我們用 git add 指令:
Untracked files:
(use "git add <file>..." to include in what will be committed)
shopping.txt
2.6 將檔案複製一份到暫存區,指令:
git add *.txt
git status
<Note> 出現下列訊息,指出 shopping.txt 已修改,尚未 commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: shopping.txt
2.7 將檔案「認可」到數據庫,指令:
git commit -m "create a shopping list"
git status
<Note> 出現下列訊息,指出 master 這個 branch 各項異動都已確認。
On branch master
nothing to commit, working directory clean
2.Q 請試著回答下列問題: (本文件的結尾處有參考答案)
(1) 什麼是 repository(數據庫)?
(2) 什麼是 working directory?
(3) 一個檔案,在 git 體系有哪三種狀態?
3. 建立新的 branch (分支)
3.1 在終端機視窗,輸入指令: git branch
<Note> 目前只有一個 branch (分支)
* master
3.1 在終端機視窗,輸入下列指令,建立 bbq 新 branch (分支):
git branch bbq
git branch
<Note> 訊息以 * 指出我們目前使用是 master 這個分支。
bbq
* master
<Note> 終端機 $ 提示字元左側也會提示我們在哪一個分支
例如: (master) $
3.2 在終端機視窗,輸入下列指令可切換分支:
git checkout bbq
git branch
<Note> 訊息以 * 指出我們目前使用是 bbq 這個分支。
* bbq
master
3.3 在左側的 Workspace,滑鼠點兩下 shopping.txt,將內容修改成這樣:
# Shpping List
- milk
- 10 eggs
- pork
- barbecue grill
- wood coal
<Note> 加上三項烤肉所需的品項,雞蛋數量為 10 顆。
<Note> 記得要存檔喔! (Hint: File | Save)
3.4 將修改後的 shopping.txt 「認可」到 repository (的 bbq 分支)
git status
git add shopping.txt
git commit -m "add bbq items"
3.5 切換回 master branch:
git checkout master
git branch
<Note> * 指出我們目前使用是 master 這個分支。
bbq
* master
3.6 在左側的 Workspace,滑鼠點兩下 shopping.txt 開啟檔案。
<Note> 為什麼剛才的那三項烤肉品項不見了?
4. 觀察/比較/合併 branch (分支)
4.1 在終端機視窗,輸入指令: git diff bbq
<Note> 此為文字版本的差異報告:
diff --git a/shopping.txt b/shopping.txt
index 2cdbce5..051e8e5 100644
--- a/shopping.txt
+++ b/shopping.txt
@@ -1,7 +1,4 @@
# Shpping List
- milk
-- 10 eggs
-- pork
-- barbecue grill
-- wood coal
+- eggs
4.2 安裝 ungit。
在終端機視窗,輸入指令: npm install -g ungit
<Note> ungit 是一套圖形介面的 git client 工具。
4.3 啟動 ungit 伺服器程式,在終端機視窗,輸入下列指令:
ungit --urlBase http://$IP --port $PORT
4.4 在瀏覽器輸入下列網址,檢視 ungit 用戶端執行畫面:
https://你的Workspace名稱-你的c9名稱.c9users.io/
例如:
https://lab-stdnt25.c9users.io/
4.5 在「Enter path to repository」輸入下列路徑,然後按下 <Enter> 鍵:
/home/ubuntu/workspace/gitLab
<Note> /home/ubuntu/workspace 是我們 workspace 的實體路徑
<Note> gitLab 是本練習的工作目錄名稱
4.6 觀察/比較 master 與 bbq 的差異(請留意老師的上課示範)
4.7 回到終端機視窗,按下 Ctrl + C 組合鍵以結束 ungit 伺服器程式。
4.8 合併 master 與 bbq。在終端機視窗,輸入下列指令:
git merge --no-ff bbq
4.9 在左側的 Workspace,滑鼠點兩下 shopping.txt 開啟檔案。
<Note> 購物清單已換成新版內容。
4.Q 請試著回答下列問題:
(1) 什麼是 branch(分支)?
(2) 每個 repository(數據庫) 預設的 branch(分支) 叫什麼名字?
(3) 哪一個指令可切換 branch(分支)?
(4) 要合併分支主線的 git 指令怎麼寫?
5. 版本衝突模擬與因應對策
5.1 確認目前使用的是 master 這個 branch:
git checkout master
5.2 在購物清單尾端加入 - mushroom 新品項
<Note> 記得要存檔喔! (Hint: File | Save)
5.3 輸入下列指令,「認可」檔案異動:
git add shopping.txt
git commit -m "add mushroom into shopping list"
5.4 切換到 bbq 這個 branch:
git checkout bbq
5.5 在購物清單尾端加入 - corn 新品項
<Note> 記得要存檔喔! (Hint: File | Save)
5.6 輸入下列指令,「認可」檔案異動:
git add shopping.txt
git commit -m "add corn into shopping list"
<Note> 好了,現在兩個 branch 各自改了內容而且都確認了。怎麼辧呢?
5.7 輸入下列指令:
git checkout master
git merge --no-ff bbq
<Note>結果出現下列訊息,提示我們有版本衝突問題:
Auto-merging shopping.txt
CONFLICT (content): Merge conflict in shopping.txt
Automatic merge failed; fix conflicts and then commit the result.
5.8 輸入指令: nano shopping.txt 介入編輯;
或者,直接點兩下 shopping.txt,Cloud9 有支援 Git 版本衝突編輯,
如果要選邊,請在「our changes」或「their changes」點按「Use Me」
如果要兩邊都留著,點按「Use Both」
<Note> 記得要存檔喔! (Hint: File | Save)
5.9 解決衝突之後,重新再 commit 一次,指令如下:
git add shopping.txt
git commit -m "Merge branch 'bbq', conflict solved"
5.A 執行下列指令,觀看整個修改歷程記錄:
git log
<Note> 鍵盤 Q 鍵可結束瀏覽畫面。
2.Q 請試著回答下列問題:
(1) 什麼是 repository(數據庫)? 一個 .git 的資料夾
(2) 什麼是 working directory? 受 Git 系統節制的工作目錄
(3) 一個檔案,在 git 體系有哪三種狀態? 已變更、已暫存、已認可