先問「內容在哪裡」,再選復原工具
尚未 staged 的修改,可用 git restore <path> 放棄;已 staged 但想保留工作內容,用 git restore --staged <path>。
已經 commit 且分享出去,通常優先使用 git revert <commit> 新增反向 commit,因為它不改寫別人已取得的歷史。
reset 會移動 branch:
--soft:保留在 Staging Area。- 預設 mixed:保留在 Working Directory。
--hard:連工作內容一起丟棄,風險最高。
看到事故時先停下來,不要連續嘗試多個指令。先用
git status、git log --oneline確認狀態,再選影響範圍最小的方法。
Ask “where is the content?” before choosing a recovery tool
Discard unstaged changes with git restore <path>. If the change is staged but you want to keep the work in your directory, use git restore --staged <path>.
For a commit that was already shared, prefer git revert <commit> to add an inverse commit because it does not rewrite history that others already have.
reset moves a branch:
--soft: keep the changes in the Staging Area.- Default mixed: keep the changes in the Working Directory.
--hard: discard the work too; this is the highest-risk option.
When something goes wrong, stop instead of trying several commands in a row. Check
git statusandgit log --oneline, then choose the smallest-impact method.