EKsumic's Blog

let today = new Beginning();

Click the left button to use the catalog.

OR

GitBash關於回滾操作

查看提交歷史,找到目標提交的哈希值:

git log --oneline

git log

 

有4種回滾方式:

git reset --soft <commit-hash>
git reset --hard <commit-hash>
git revert <commit-hash>
git checkout <commit-hash>

 

1. git reset --soft

影響: 將 HEAD 指針回滾到指定的提交,但工作目錄和暫存區(staging area)保持不變。

結果: 提交歷史會被修改,但更改仍然在工作目錄中,之前的提交不會從提交歷史中消失,只是 HEAD 指針移動到了指定的提交。

命令:

git reset --soft <commit-hash>

 

2. git reset --hard

影響: 將 HEAD 指針回滾到指定的提交,工作目錄和暫存區都會被重置到指定提交的狀態。

結果: 提交歷史會被修改,之前的提交從當前分支的提交歷史中消失,工作目錄和暫存區的內容也會被重置。

命令:

git reset --hard <commit-hash>

 

3. git revert

影響: 創建一個新的提交,撤銷指定提交的更改。

結果: 提交歷史不會被修改,之前的提交不會消失。相反,會創建一個新的提交,表示撤銷操作。

命令:

git revert <commit-hash>

 

4. git checkout

影響: 切換到指定的提交,但不修改提交歷史。

結果: 提交歷史不會被修改,之前的提交不會消失。HEAD 指針會指向指定的提交,您處於分離的 HEAD 狀態。

命令:

git checkout <commit-hash>

 

 

→上一篇:GitBash關於rebase的操作

→下一篇:GitBash 切換到特定提交後修改並push到遠端

→返回目錄:GitBash使用手冊

This article was last edited at 2024-07-07 05:47:15

* *