~/教學/GITHUB.md

文章 / Claude Code

Claude Code 把專案改壞了?git commit、git restore、git push 三個指令救回來

/rewind 追不到用指令改的檔案,真正的存檔要靠 Git:能跑就 commit,改壞就 restore,再 push 到 GitHub 備份。

Claude Code 改著改著把專案改壞了,不用整個重來。這篇是 Reels 裡 GITHUB 那支的完整版:三個 git 指令,一個存檔、一個救回、一個備份到 GitHub。畫面上的輸出都是我 2026-11-17 在一個小專案真的跑出來的,官方說明也在同一天確認過。

01你會得到什麼:改壞了也能一鍵回到能跑的版本

Reels 畫面:終端機 git status --short 列出四個被改壞的檔案,標成紅色
Reels 畫面:Claude Code 改壞了四個檔案。

學完你會:

  1. 在專案能跑的時候存一個檔(commit)。
  2. 改壞時一個指令全部回到上一個存檔(restore)。
  3. 把存檔備份到 GitHub,電腦壞了也還在(push)。
  4. 知道為什麼 Claude Code 的 /rewind 不能取代 Git。
Reels 畫面:官方文件 Bash command changes not tracked 段落,/rewind 被圈起來
Reels 畫面:/rewind 不是存檔,官方說要用 Git。

02為什麼不能只靠 /rewind

Claude Code 有 /rewind(按兩下 Esc 也可以),能退回 Claude 用編輯工具改過的檔案。但官方 checkpointing 文件寫得很清楚:只被 Bash 指令改過的檔案追不到,要還原請用版本控制,例如 Git。官方也說 checkpoint 不能取代版本控制。

Claude Code 官方 checkpointing 文件:Bash command changes not tracked 段落
官方文件原文(2026-11-17 截圖)。

03第 0 步:裝 Git,專案資料夾 git init(只做一次)

  1. 到 git-scm.com/downloads 下載安裝 Git。
  2. 在終端機進到你的專案資料夾。
  3. 打 git init,這個資料夾就開始用 Git 管理。
只做一次
git init

04第 1 步:能跑就存檔(git commit)

每次確認專案能跑,就存一個檔。訊息寫你看得懂的就好:

存檔
git add .
git commit -m "能跑的版本"
終端機:git add . 和 git commit -m 能跑的版本,輸出 root-commit 0e65fc2,4 files changed
真實輸出:第一個存檔 0e65fc2。

05第 2 步:又改壞了,git restore 回到上一個存檔

終端機:git status --short 顯示 README.md、index.html、src/app.js、src/style.css 被改
真實輸出:四個檔案被改壞。
全部回到上一個存檔
git restore .
終端機:git restore . 之後 git status 顯示 nothing to commit, working tree clean
真實輸出:working tree clean,回到能跑的版本。

06第 3 步:git push 備份到 GitHub

  1. 到 github.com/new 開一個 repo(可以選 Private)。
  2. 照新 repo 頁面上的指示,把 git remote add origin … 那幾行貼到終端機,只做一次。
  3. 之後每次 commit 完,打 git push。
每次存檔後
git push
Reels 畫面:筆電螢幕裂開,旁邊 GitHub 卡片寫 0e65fc2 能跑的版本 還在
Reels 畫面:就算電腦壞了,GitHub 上的存檔還在。

07要注意

  1. git restore . 會直接丟掉還沒 commit 的改動,按下去之前確定那些真的不要了。
  2. 密碼、API 金鑰這類檔案(例如 .env)不要 commit,先寫進 .gitignore。
  3. /rewind 還是好用,但只當作 Claude 對話裡的快速復原,真正的存檔交給 Git。
  4. 指令和官方說明以 2026-11-17 為準。

資料來源

延伸閱讀