[教學] git應用-後悔藥有得買2 - 討論區

[教學] git應用-後悔藥有得買2

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

特種兵

特種兵圖像(預設)

2020-08-22 06:31:53

From:211.23.21.202

在 git 的世界裡面基本上沒有什麼救不回來的狀況,也就是後悔藥是有得買的。

在之前的章節中多少有提過,例如如何從 status 的狀態放棄,或者 add 加入暫存區的檔案如何排除,

以及已經 commit 的檔案如何修改等等。

這裡稍微整理各種狀況讓大家在出問題時可以先看看這兩篇,找尋你的後悔藥。

從暫存檔中移除

那如果已經加到暫存檔,也就是 add 進去了,有辦法後悔嗎?

當然沒問題,我們看一下目前暫存區的狀態

$ git status                                                                    
 On branch newnew                                                                
 Changes to be committed:                                                        
   (use "git restore --staged <file>..." to unstage)                             
         modified:   abc.txt                                                     
         deleted:    log.txt                                                     
         new file:   qqq/abc.txt                                                 

看起來就是修改又刪除了一些檔案,然後產生了一個新的資料夾及檔案,並且加到暫存區(stage)了,

由 git 給我們的資訊很簡單的發現,只要使用 git restore --staged 檔名 就可以。

$ git restore --staged abc.txt                                                  

$ git status                                                                    
 On branch newnew                                                                
 Changes to be committed:                                                        
   (use "git restore --staged <file>..." to unstage)                             
         deleted:    log.txt                                                     
         new file:   qqq/abc.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:   abc.txt                                                     

所以 abc.txt 已經從暫存區移出,如果想從 status 也還原,那就參考 上一篇 的教學吧。

我們先把 abc.txt 再次 add 進 stage 然後試看看用 . 能不能一次全部從 stage 移出:

$ git add abc.txt

$ git restore --staged .

$ git status                                                                    
 On branch newnew                                                                
 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)         
         modified:   abc.txt                                                     
         deleted:    log.txt                                                     

 Untracked files:                                                                
   (use "git add <file>..." to include in what will be committed)                
         qqq/                                                                    

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

成功了,這個狀態應該要很熟悉,也就是類似回到上一篇的狀況。

但是,有沒有一種方法是可以直接從暫存區就直接恢復,而不是只退到工作目錄而已,

因為如果想直接還原到沒有動作,依上例就必須先從暫存區移回工作目錄,再由工作目錄還原到未更動的狀態。

當然我們可以把這兩篇的還原指令用 && 連起來完成,不過這不是我們想要的方法。

大家可以回想一下 git 的架構,其實這個問題就是還原到上一次提交的狀態而已,

因為我們還沒有做新的 commit 所以只要回到上一個提交的狀態,後面的變化全部都會被取消。

$ git reset --hard HEAD

從 commit 中移除

這部分我們之前的基礎應用都講過了,例如添加 commit 的資訊,或改寫 commit 內容,

還有刪除、拆解、合併 commit 等等,這邊就不多說了。