首頁·終端機命令

git restore

git

用法

git restore [--staged] [-s <commit>] <pathspec>

git 2.23 引入的命令,用來還原檔案內容;預設會覆蓋工作區的檔案,未提交的改動就沒了,而 --staged 只是取消暫存。

常用選項

選項含義
--stagedunstage the file but keep your edits
--worktreereset the file on disk; this is the default
--staged --worktreeboth: throw the edits away and unstage
-s <commit>take the content from that commit instead of the index
-pchoose hunk by hunk

範例

git restore src/app.ts

wipes uncommitted edits to that file, with no way back

git restore --staged src/app.ts

takes it out of the staging area, edits intact

git restore -s HEAD~2 src/app.ts

brings back the version from two commits ago

難的地方在於復原有好幾種:把分支的指向挪走,和補一個反向提交,不是一回事。

怎麼看

  • 方括號 [ ] 表示這部分可以不寫。
  • 省略號 … 表示可以寫多個。
  • 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。

常見問題

Q. git restore 是做什麼的?

git 2.23 引入的命令,用來還原檔案內容;預設會覆蓋工作區的檔案,未提交的改動就沒了,而 --staged 只是取消暫存。

Q. 怎麼寫?

git restore [--staged] [-s <commit>] <pathspec> —— 方括號表示可以省略的部分。

Q. 值得記的選項有幾個?

這裡列了 5 個,完整列表在 man git。這條命令屬於git。

相關命令

man 手冊: man git-restore