iT邦幫忙

1

Git-解讀git status的訊息,了解工作資料夾的版控狀態

git
WM 2019-05-10 16:36:1811611 瀏覽
  • 分享至 

  • xImage
  •  

這是一個新建的Angualr專案,完全沒有任何異動。
https://ithelp.ithome.com.tw/upload/images/20190510/20112573akG5ITv8vY.png

初始的git狀態

輸入git status
https://ithelp.ithome.com.tw/upload/images/20190518/20112573WHx9bRP6pC.png
看看git status所顯示的訊息是什麼。

on branch master

表示目前所在的分支(master分支),預設分支。

nothing to commit, working tree clean

表示目前沒有任何異動需要commit,這邊的commit是指每次異動的版本,或稱作節點。
working tree clean表示目前正在工作的資料夾非常乾淨,因為完全沒有任何增刪修。

刪除檔案

刪除3個檔案
https://ithelp.ithome.com.tw/upload/images/20190510/20112573P2z089ziTV.png

透過git status,看看訊息有何變化。
https://ithelp.ithome.com.tw/upload/images/20190518/20112573vCJkRiEMEM.png

Changes not staged for commit:
表示,這次所變更的資料(紅字),還無法加入到這次的commit之中。
檔名前面的deleted,表示這次的變更是刪除檔案。

如果執行commit的話,會失敗,並收到錯誤訊息。
https://ithelp.ithome.com.tw/upload/images/20190518/20112573fykgHfhBvh.png
訊息會再次警告,這次的改變並沒有加入commit之中(no changes added to commit),從log可以看出,確實沒有commit。

回到剛剛git status那張圖,git已經有給提示:
(use "git add/rm <file>..." to update what will be committed)
表示可以透過add(新增)/rm(移除)檔案,將這次的異動update至commit中。

使用add指令只將README.md加入commit之中。
https://ithelp.ithome.com.tw/upload/images/20190519/20112573PIpS5qpiRk.png
add完之後,再次輸入git status,發現訊息有所改變:
Changes to be committed:
表示刪除README.md的操作(綠色),準備加入commit之中。
但下面的2個檔案操作(紅色),依舊是同樣的警告。

再次執行commit。
https://ithelp.ithome.com.tw/upload/images/20190510/20112573PcHtyQz16T.png
確實建立一個只包含這次刪除README.md的commit。

執行git status。
https://ithelp.ithome.com.tw/upload/images/20190510/20112573VGgnjGWC06.png
只剩下未加入commit的資料刪除操作。
至於刪除README.md的操作已加入commit之中,所以這次就不會再顯示了。

接下來,把剩下的操作都加入commit,這次在add之後空一格再加點(.),表示將所有的異動操作都加入。
https://ithelp.ithome.com.tw/upload/images/20190510/20112573kvirTHXPkm.png
OK!已成功加入。
但,如果想回復這次的操作呢?

取消已被add的檔案

git有給提示:(use "git reset HEAD <file>..." to unstage)
將所有的異動,回復到unstage狀態。
<file>,檔名是選擇性輸入。
HEAD表示指向目前工作目錄的最新版本,最近一次的commit。
https://ithelp.ithome.com.tw/upload/images/20190510/20112573dNYBhvm3V5.png
此時,又回復到剛剛的狀態。
該指令,只是回復異動狀態,無法改變資料夾的檔案結構,刪除的檔案不會回復。

救回誤刪的檔案

如果,真的手殘,把檔案誤刪,git也有辦法將檔案救回來。
https://ithelp.ithome.com.tw/upload/images/20190510/20112573wfTVbBIe2r.png
(use "git checkout -- <file>..." to discard changes in working directory)
表示我們可以取消這次的變更(discard changes),在工作目錄中(working directory)

將tslint.json救回來
https://ithelp.ithome.com.tw/upload/images/20190510/201125735XZqjbdGct.png
目前只剩一個檔案的操作是unstage狀態

從資料夾看出,檔案確實救回來了
https://ithelp.ithome.com.tw/upload/images/20190510/20112573YHPQrBuXVe.png

回復修改的程式碼

現在,我們來修改tslint.json內容,將每行長度改為150。

"max-line-length": [
      true,
      150
    ]

異動的操作狀態變成modified
https://ithelp.ithome.com.tw/upload/images/20190510/20112573bg9GlFmiUs.png

萬一把檔案改壞,反悔了,一樣可以使用git checkout -- tslint.json回復修改前的內容
https://ithelp.ithome.com.tw/upload/images/20190510/20112573OpDhkUTIeX.png
只剩一個unstage狀態。
這時剛剛修改的內容,會回復到最初的140。

"max-line-length": [
      true,
      140
    ]

結論

看懂git status的訊息提示,是學好git的第一步,
在遇到問題時,才不會毫無頭緒,不知如何解決。

本文為觀看網路教學的學習筆記。


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
來杯拿鐵
iT邦新手 2 級 ‧ 2019-05-10 20:09:36

很仔細的講解,想看更多有關於git指令文章。

WM iT邦新手 3 級 ‧ 2019-05-11 10:56:43 檢舉

謝謝

我要留言

立即登入留言