[教學] git-26 插入 commit - 討論區

[教學] git-26 插入 commit

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

特種兵

特種兵圖像(預設)

2020-04-13 19:19:13

From:211.23.21.202

想要在兩個 commit 之間再增加新的 commit 也可以

其實做法跟拆解 commit 的動作差不多

都是利用 rebase 的 edit 來進行的

# 看一下現在的前 5 個 log
$ git log --oneline -5                                                          
 3d914e7 (HEAD -> master) 複製一個 test.py 蓋掉原本的同名檔案                    
 6dd175d 在 log.txt 加了第三行                                                   
 316ffbc 在 hello.txt 加入第3行                                                  
 d78c4ff 刪掉生活班232小時課表--0610.doc 與 abc.txt                              
 d7de1ff 我也加了生活班232小時課表--0610.doc                                     

我想在 6dd175d316ffbc 之間再加兩個 commit

那我們就把 rebase 設在更早一個的 d78c4ff 上:

$ git rebase -i d78c4ff
# 並且把 316ffbc 的 pick 改成 edit 存檔離開
# 再次提醒, rebase -i 的順序跟 log 是相反的,我們要編輯的是比較早的那一個
edit 316ffbc 在 hello.txt 加入第3行                                                  
pick 6dd175d 在 log.txt 加了第三行                                                   
略

接著就是做要新增 commit 的事情:

# 刪掉 ooqq.txt
$ rm -f ooqq.txt                                                                

$ git status                                                                    
 interactive rebase in progress; onto d78c4ff                                    
 Last command done (1 command done):                                             
    edit 316ffbc 在 hello.txt 加入第3行                                          
 Next commands to do (2 remaining commands):                                     
    pick 6dd175d 在 log.txt 加了第三行                                           
    pick 3d914e7 複製一個 test.py 蓋掉原本的同名檔案                             
   (use "git rebase --edit-todo" to view and edit)                               
 You are currently splitting a commit while rebasing branch 'master' on 'd78c4ff'
 .                                                                               
   (Once your working directory is clean, run "git rebase --continue")           

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

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

# 接下來的動作不用我多說了吧
$ git add ooqq.txt                                                              

$ git commit -m '刪除 ooqq.txt'                                                 
 [detached HEAD 13a95ad] 刪除 ooqq.txt                                           
  1 file changed, 1 deletion(-)                                                  
  delete mode 100644 ooqq.txt                                                    

有看到 detached HEAD 這個斷頭關鍵字吧,因為現在我們算是處在 rebase 的其中一個臨時空間中,

並不是在一般的 HEAD 指向上,所以會有這個提示字樣。

# 再看一下狀態
$ git log --oneline -3                                                          
 13a95ad (HEAD) 刪除 ooqq.txt                                                    
 316ffbc 在 hello.txt 加入第3行                                                  
 d78c4ff 刪掉生活班232小時課表--0610.doc 與 abc.txt                              

# 新增一個 qqoo.txt 檔案再做一個 commit
$ touch qqoo.txt                                                                

$ git add .                                                                     

$ git commit -m '新增 qqoo.txt'                                                 
 [detached HEAD 9a73d0c] 新增 qqoo.txt                                           
  1 file changed, 0 insertions(+), 0 deletions(-)                                
  create mode 100644 qqoo.txt                                                    

# 再看一下狀態
$ git log --oneline -3                                                          
 9a73d0c (HEAD) 新增 qqoo.txt                                                    
 13a95ad 刪除 ooqq.txt                                                           
 316ffbc 在 hello.txt 加入第3行                                                  

應該沒問題,那就完成後續的 rebase 吧

$ git rebase --continue                                                         
 Successfully rebased and updated refs/heads/master.                             

# 看到 Successfully 關鍵字就可以收工囉 
$ git log --oneline -5                                                          
 aee4e62 (HEAD -> master) 複製一個 test.py 蓋掉原本的同名檔案                    
 0338589 在 log.txt 加了第三行                                                   
 9a73d0c 新增 qqoo.txt                                                           
 13a95ad 刪除 ooqq.txt                                                           
 316ffbc 在 hello.txt 加入第3行                                                  

沒問題,再次成功動了手腳。