[教學] git-17 部分提交 - 討論區

[教學] git-17 部分提交

文章瀏覽次數 1922 文章回覆數 0

特種兵

特種兵圖像(預設)

2020-03-13 18:36:27

From:211.23.21.202

如果某個檔案只想提交某些內容上去,而不是整個檔案的話也做得到。

這個會發生在某個檔案的某個區塊還沒寫好,但其他區塊必須被使用的狀況。

例如修掉了 A bug 接著正在寫 B 功能還沒寫完,但主管說這個修掉的 bug 很棘手,

必須先推上去正式版讓大家使用,那為了怕還沒寫完的功能也一起上架了,就先提交已經修掉 bug 的那部分程式即可。

當然針對這個狀況會有更好的做法,但我們只是舉例。

先來看一下 hello.txt 這個檔的內容:

$ cat hello.txt                                                                 
 你好                                                                            
 我很好                                                                          
 測試                                                                            
 以下這兩行不要提交                                                              
 第一行                                                                          
 第二行                                                                          
 這邊是要提交的下半部                                                            

好,假設我們現在不想把那兩行提交(第4與5行),那該怎麼做呢?

此時需要使用參數 -p 來提交再進行編輯,把不想提交的部分刪掉。

# 目前是編輯完 hello.txt 還沒加到暫存區
$ git status                                                                                                                                                                                    
 On branch master                                                                                                                                                                                
 Changes not staged for commit:                                                                                                                                                                  
   (use "git add <file>..." to update what will be committed)                                                                                                                                    
   (use "git restore <file>..." to discard changes in working directory)                                                                                                                         
         modified:   hello.txt                                                                                                                                                                   

 no changes added to commit (use "git add" and/or "git commit -a")                                                                                                                               

$ git add -p hello.txt                                                          
 diff --git a/hello.txt b/hello.txt                                              
 index a660351..3f9701b 100644                                                   
 --- a/hello.txt                                                                 
 +++ b/hello.txt                                                                 
 @@ -1,3 +1,4 @@                                                                 
 +                                                                               
  你好                                                                           
  我很好                                                                         
  測試                                                                           
 (1/1) Stage this hunk [y,n,q,a,d,e,?]? e                                           
# 如果回答 y 就是整個檔案提交,等於是沒有加 -p 的效果
# 我們回答 e 就可以進行編輯
# 他會以預設編輯器把 hello.txt 打開
# 我們刪掉第4與5行之後存檔離開

# Manual hunk edit mode -- see bottom for a quick guide.
@@ -1,3 +1,6 @@
 你好
 我很好
+以下兩行不想提交
+第一行
+第二行
 測試
# ---
# 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.

# 有加號的部分是我們最新新增的行但還沒提交的狀況
# 刪掉存檔離開後回到 git bash
$ git status                                                                                                                                                                                    
 On branch master                                                                                                                                                                                
 Changes to be committed:                                                                                                                                                                        
   (use "git restore --staged <file>..." to unstage)                                                                                                                                             
         modified:   hello.txt                                                                                                                                                                   

 Changes not staged for commit:                                                                                                                                                                  
   (use "git add <file>..." to update what will be committed)                                                                                                                                    
   (use "git restore <file>..." to discard changes in working directory)                                                                                                                         
         modified:   hello.txt                                                                                                                                                                   

$ cat hello.txt                                                                                                                                                                                 
 你好                                                                                                                                                                                            
 我很好                                                                                                                                                                                          
 以下兩行不想提交                                                                                                                                                                                
 第一行                                                                                                                                                                                          
 第二行                                                                                                                                                                                          
 測試                                                                                                                                                                                            

# 在工作目錄的 hello.txt 檔案還是完整的
$ git commit -m '部分提交練習'                                                                                                                                                                  
 [master 362f466] 部分提交練習                                                                                                                                                                   
  1 file changed, 1 insertion(+)                                                                                                                                                                 

$ git status                                                                                                                                                                                    
 On branch master                                                                                                                                                                                
 Changes not staged for commit:                                                                                                                                                                  
   (use "git add <file>..." to update what will be committed)                                                                                                                                    
   (use "git restore <file>..." to discard changes in working directory)                                                                                                                         
         modified:   hello.txt                                                                                                                                                                   

 no changes added to commit (use "git add" and/or "git commit -a")                                                                                                                               

明明已經提交,但為什麼暫存區告訴我們 hello.txt 被修改了?沒錯,因為我們把不要提交的部分刪掉了,

所以不想提交的部分就被丟到暫存區囉,我們仔細看一下 hello.txt 的紀錄

$ git blame hello.txt                                                                                                                                                                           
 ^0fdb52f welcome.txt (Logo Kuo          2020-02-16 13:01:25 +0800 1) 你好                                                                                                                       
 ^0fdb52f welcome.txt (Logo Kuo          2020-02-16 13:01:25 +0800 2) 我很好                                                                                                                     
 362f4669 hello.txt   (Logo Kuo          2020-03-08 23:06:35 +0800 3) 以下兩行不想提交                                                                                                           
 00000000 hello.txt   (Not Committed Yet 2020-03-08 23:08:27 +0800 4) 第一行                                                                                                                     
 00000000 hello.txt   (Not Committed Yet 2020-03-08 23:08:27 +0800 5) 第二行                                                                                                                     
 b7df6113 hello.txt   (Logo Kuo          2020-02-28 21:31:22 +0800 6) 測試                                                                                                                       

由上面的紀錄可以看到最前面 00000000 開頭的那兩行,有標上 not commited yet 的字樣,

這樣就可以知道這幾行並沒有被提交上去,正合我們的意思。