iT邦幫忙

2021 iThome 鐵人賽

DAY 15
1
Modern Web

【Git】從零開始學習 Git - 30 天的學習筆記系列 第 15

Day15|【Git】git reset 補充 - 三種模式

之前介紹 git reset 時,有提到說使用 git reset 指令時,有三種常用來搭配的參數模式,因此本篇就來詳細說明這三種模式分別是什麼意思。


💡 git reset - Reset current HEAD to the specified state

意思為可以使 HEAD 指標指向其他地方。

這些模式代表被 reset 出來的 Commit 其 HEAD 與 Branch 會指向何處,另外工作目錄與暫存區中的檔案會有什麼改變。


# Mixed 模式(預設)

當 git reset 後面沒加參數時,預設為 mixed 模式

$ git reset **--mixed** [commit]
//同 git reset [commit]

此模式會將暫存區的檔案丟掉,但不會影響到工作目錄的檔案。也就是說我們指令中的 commit 除了移動 HEAD 及 Branch 外,也會從暫存區移出,但會留在工作目錄。

🛠 實際操作

假設現在有以下這些 Commit 版本

https://ithelp.ithome.com.tw/upload/images/20210929/20141010lKNeTAfeTU.png

我還原到 a8bf179 這個版本(使用 master^ 方式),並用 git status 指令查看檔案狀態。

$ git reset master^ # 還原到目前 master 前一個版本
$ git status # 查看檔案狀態

https://ithelp.ithome.com.tw/upload/images/20210929/20141010Nx0nrK5sw0.png

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

訊息回饋:no changes added to commit ,代表目前檔案還沒有加至暫存區,如果之後要提交,需要再次使用 git add 指令,將檔案加至暫存區後並提交。

# Sort 模式

$ git reset **--soft** [commit]

使用 Sort 模式的 reset,拆出來的 Commit 只是單純移動 HEAD 與其 Branch,暫存區與工作目錄中的檔案並**不會被丟棄**

🛠 實際操作

假設現在有以下這些 Commit 版本

https://ithelp.ithome.com.tw/upload/images/20210929/20141010Rz9lgvq82z.png

同先前使用過的方式,取得 a8bf179 版本,只是這次有加上 --soft 的模式。

$ git reset master^ --soft
$ git status

https://ithelp.ithome.com.tw/upload/images/20210929/20141010Td75gAYfji.png

Changes to be committed

訊息回饋:Changes to be committed ,代表目前檔案被放置在暫存區,並等待被提交

# Hard 模式

$ git reset **--hard** [commit]

使用 Hard 模式的 reset,除了移動 HEAD 與其 Branch 外,暫存區與工作目錄中的檔案也都會被移出

🛠 實際操作

假設現在有以下這些 Commit 版本

https://ithelp.ithome.com.tw/upload/images/20210929/20141010v3putTYiZl.png

同先前使用過的方式,取得 a8bf179 版本,改成加上 --hard 的模式。

$ git reset master^ --hard
$ git status

https://ithelp.ithome.com.tw/upload/images/20210929/201410104OKUUzINUO.png

nothing to commit, working tree clean

訊息回饋:

nothing to commit - 目前沒有任何異動需要commit

working tree clean - 目前工作目錄非常乾淨,沒有任何增刪修。


將以上觀念使用表格、圖示整理:

https://ithelp.ithome.com.tw/upload/images/20210929/201410106vURH0WyY7.png

https://ithelp.ithome.com.tw/upload/images/20210929/20141010KvUz58KCHc.png


上一篇
Day14|【Git】檔案管理 - 檔案還原 git reset
下一篇
Day16|什麼是 HEAD ?
系列文
【Git】從零開始學習 Git - 30 天的學習筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言