git restore
這是 Google 對 https://www.talktocomputer.site/blogs/78/ 的緩存。
git 2.23 版本新增了switch、restore命令,因為git checkout 命令職責較多、不夠明確,而restore命令則專門用來還原、撤銷修改等,我們這里來總結下git restore 命令的一些常用的用法。另外,這個命令在2.27.0版本還是實驗性的,將來行為可能會改變。
git restore 撤銷文件工作區的修改
命令:git restore --worktree <path>
舉例:git restore --worktree test2.txt
tips:如果暫存區有該文件的修改,恢復到和暫存區一致;如果暫存區沒有該文件,會將工作區的文件恢復到和最近的提交的一致。–worktree 也可以省掉,即:git restore <path>
git restore 撤銷工作區所有文件的修改
命令:git restore .
git restore 撤銷暫存區的修改,將文件恢復到工作區去
命令:git restore --staged <path>
舉例:git restore --staged test2.txt
舉例2:git restore --staged ‘*.txt’
tips:git restore 撤銷暫存區某些文件的修改,將這些文件恢復到工作區去 https://blog.csdn.net/u013493841/article/details/104451987
git restore 將工作區內容切換到上個 commit 版本
命令:git restore --source=HEAD~1 .
git restore 將工作區內容切換到某個版本庫去
命令:git restore --source=<commit> .
舉例:git restore --source=6271090 .
tips:這個命令是將當前工作區的內容都恢復到了版本6271090,但是不會改變歷史記錄,如果有提示需要有刪除的文件等,可以使用git add,然后提交生成新的提交記錄,這點和revert有些類似,但是也有不同,revert可能會生成多個重做的記錄,但是restore不會生成新的記錄,如果有文件增刪改動可能需要自己做add和commit操作。
參考資料:
https://git-scm.com/docs/git-restore
還原有三種情況:
只是修改了文件,沒有任何 git 操作
修改了文件,並提交到暫存區(即:編輯之后,進行git add 但沒有 git commit -m "留言xxx")
修改了文件,並提交到倉庫區(即:編輯之后,進行git add 並且 git commit -m "留言xxx")
如果是情況1:
git checkout -- aaa.html // 指定還原`aaa.html`文件
git checkout -- * // 還原所有文件
如果是情況2:
git log --oneline // 可以省略
git reset HEAD // 回退到當前版本
git checkout -- aaa.html
如果是情況3:
git log --oneline // 可以省略
git reset HEAD^ // 回退到上一個版本,注意看HEAD后面有個 ^ HEAD^ 是回退到上個版本 HEAD^^ 是回退到上上個版本 HEAD~數字 是回退到數字個版本
git checkout -- aaa.html
