[教學] git應用-還沒寫完但要關門了 - 討論區

[教學] git應用-還沒寫完但要關門了

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

特種兵

特種兵圖像(預設)

2020-10-05 10:51:30

From:211.23.21.202

這個情境跟之前提過的 stash 是不同的,stash 是說專案開發到一半要去修 bug 或要切到其他分支去處理事情。

等處理好後再把 stash 接回來繼續作業,避免未完成品必須要先推上去的玕尬狀況。

但這次真的是非推上去不可,昨天寫 code 到九點多還想把最後一部分寫完時,關門的同仁不能等我了,

雖然我也有拿鑰匙啦,不過想說也實在有點晚了,明天休一整天還可以慢慢在家裡寫,就只好先推上去了。

不推不行,因為我公司筆電沒開遠端,不推上去回家就沒搞頭了。

commit 訊息就加個未完待續之類的提醒自己,重點是回家之後要怎麼繼續呢?

首先,當然是先把專案拉回來,看到最後一個未完待續的 commit 以後,把它丟回工作目錄就可以繼續寫了。

等到都寫好後再推上去就行囉。

這樣還是保持一個 commit 且是完整的工作內容。

$ git log -2 --oneline                                                          
 501ace2 (HEAD -> logo, origin/logo) 修正收文修改頁面顯示附件(js未完)            
 25a4c4d (origin/master, origin/HEAD, master) 讓前後臺登入後可以顯示登入者名字   

最後,也就是最上面的 commit 還沒完成

所以,我們是要再往前退一個版本,這樣退回的那個版本之後的紀錄全部都會被丟回工作目錄

也就是說,現在如果有 7 個 commit, 我選擇退到第 3 個 commit 後,

那麼 4, 5, 6, 7 這四個 commit 的紀錄全部會被丟到工作目錄,

假設你想合併這四個 commit 推成一個 commit 那可以這樣做。

 $ git reset 25a4c4d                                                             
 Unstaged changes after reset:                                                   
 M       xxx/controllers/documentController.php                                  
 M       xxx/models/documentModel.php                                            
 M       xxx/self/settings.php                                                   
 M       xxx/static/js/assign_document.js                                        
 M       xxx/template/document/assign_document.tpl                               

$ git status 
 On branch logo                                                                  
 Your branch is behind 'origin/logo' by 1 commit, and can be fast-forwarded.     
   (use "git pull" to update your local branch)                                  

 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:   xxx/controllers/documentController.php                      
         modified:   xxx/models/documentModel.php                                
         modified:   xxx/self/settings.php                                       
         modified:   xxx/static/js/assign_document.js                            
         modified:   xxx/template/document/assign_document.tpl                   

 no changes added to commit (use "git add" and/or "git commit -a")               
# 果然都被丟回工作目錄了
$ git log -1 --oneline                                                          
 25a4c4d (HEAD -> logo, origin/master, origin/HEAD, master) 讓前後臺登入後可以顯 
 示登入者名字                                                                    
# 沒錯,停在之前完成的 commit 上了

接下來就是一般寫程式的流程,最後按一般步驟提交上去就好了。