學習 Git (3) - 開始版控的最後一步:Commit


Posted by Calon on 2022-05-01

到目前為止我們已經將檔案加到 Git 的暫存區了,但我們還沒將這個版本儲存到 Git 的儲存庫(Repository)。要將暫存區的檔案儲存下來我們要使用 git commit

$ git commit -m '新增 helloWorld.txt'
[master (根提交) f0bb2e9] 新增 helloWorld.txt
1 file changed, 1 insertion(+)
 create mode 100644 helloWorld.txt

參數 -m 為設定說明文字,參數後面的 '新增 helloWorld.txt' 為「這次做了什麼?」,不用太長,簡單清楚就好,重點是要清楚讓別人看到時可以知道你做了哪些變動。

到這裡我們將暫存區的檔案存放到儲存庫,也就是說我們完成了一個備份,也建立了我們第一個「版本」。

※注意:Commit 只會將有加入暫存區的檔案進行處理,也就是有使用 git add 的檔案在使用 git commit 就會被存放到儲存區,沒有使用 git add 加入到暫存區的檔案則不會做任何處理。

如果在上一篇設定基本設定沒有跟著設定使用者名稱、Email 的話,這時會跳出訊息:Please tell me who you are.,這時只要設定使用者名稱、Email 就可以解決。(注意:兩個資料都需要設定,只設定其中一個還是會報錯。)

省略 git add 步驟

剛才不是說沒有加入暫存區的檔案不會被 Commit 存放到儲存庫嗎?

沒錯,但是指還沒被 Git 所追蹤的檔案的話,如果今天如果是將檔案已經加入過儲存過得檔案 Commit 上去的話,可以加上參數 -a

$ git commit -a -m '修正 helloWorld.txt'

但是如果是尚未被追蹤的檔案,也就是 Untracked 的狀態的檔案還是要先經過完整的流程。

為什麼 Git 不直接使用 Commit 指令將檔案直接存進儲存庫?

雖然要先 add 再 commit 很麻煩,但這有助於增加在閱讀 commit 的易讀性,如果今天一個檔案就一個 commit 的話,其他人或是自己要回去找多個修改紀錄就需要一個個去看,而如果今天先將所要有變動的檔案集中在一個地方再一起存放到儲存庫,這樣在往後閱讀有整理的修改紀錄時會方便許多。

可以把 git addgit commit 的這個動作想像成是在逛國外購物網站的時候,為了節省國際運費,會先將來自不同賣家的商品運送到一個集中倉庫,再一起使用空運或海運寄出。

為什麼我輸入 git commit 後跳出一個視窗並且跳不回去終端機畫面?

在使用 Commit 指令時如果址輸入 git commit 就會會跳出預設的編輯器 Vim(或者是 vi),Vim 因為操作上跟一般編輯器有很大的不同,對於沒接觸過的人不是很有善,當遇上這種狀況的時候可以直接關掉終端機再開新的先輸入 : 再輸入 q 就可以離開 Vim 回到終端機了。

順帶一提,學習 Git 系列文章都是用 Vim 寫的~

說到 Vim 就不得不提 Vim 的經典問題:怎麼離開 Vim (How do I exit the Vim editor?
至經為止已經有兩百六十萬左右的瀏覽數

只 commit 檔案裡面一部分

假設今天我們有一個 test.txt,裡面有以下文字內容:

It's for practicing.
test, 1, 2, 3.

但我第二段還沒有寫完,所以只想先 commit 第一段,這時我們可以先使用 git add,並帶上參數 -p

$ git add -p test.txt

# 執行後會出現以下文字
diff --git a/test.txt b/test.txt
index e69de29..81104b8 100644
--- a/test.txt
+++ b/test.txt
@@ -0,0 +1,2 @@
+It's for practicing.
+test, 1, 2, 3.
(1/1) Stage this hunk [y,n,q,a,d,e,?]?

最後一段的 [y,n,q,a,d,e,?] 是我們可以執行的指令,在這裡先輸入 ? 來查看這些指令:

y - stage this hunk # 將整個區塊加到暫存區
n - do not stage this hunk # 整個區塊不加到暫存區
q - quit; do not stage this hunk or any of the remaining ones # 離開,並且不會將整個區塊或是其他任何一個區塊加入暫存區
a - stage this hunk and all later hunks in the file # 把現在這個檔案裡面的區塊及之後的區塊都加入到暫存區
d - do not stage this hunk or any of the later hunks in the file # 不會把現在這個檔案裡面的區塊及之後的區塊都加入到暫存區
e - manually edit the current hunk # 手動編輯現在這個區塊
? - print help # 顯示幫助提示

我們需要將這個 hunk 加入暫存區但不是完整的 hunk,所以在這邊使用 e 去編輯要加入的部份:

# Manual hunk edit mode -- see bottom for a quick guide.
@@ -0,0 +1 @@
+It's for practicing.
+test, 1, 2, 3.
# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.
#
# If the patch applies cleanly, the edited hunk will immediately be
# marked for staging.
# If it does not apply cleanly, you will be given an opportunity to
# edit again.  If all lines of the hunk are removed, then the edit is
# aborted and the hunk is left unchanged.

我們只要將 test, 1, 2, 3. 這行刪除掉,Git 就不會將這行加入暫存區了:

# Manual hunk edit mode -- see bottom for a quick guide.
@@ -0,0 +1,2 @@
+It's for practicing.

接著輸入 :wq 離開,在執行 git status 查看:

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified: test.txt

Changes not statged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified: test.txt

發現會出現兩個 test.txt 檔案,表示我們第一段已經加進暫存區了,而第二段沒加進去,接著我們把它 commit 上去就可以只將第一段加到儲存庫了。


參考資料
  • 高見龍,《為你自己學 Git》

#Git







Related Posts

Ajax

Ajax

Source map 運作原理

Source map 運作原理

[ 紀錄 ] 實戰練習 - 抽獎程式 (以 Express 實作後端 API )

[ 紀錄 ] 實戰練習 - 抽獎程式 (以 Express 實作後端 API )


Comments