閱界食譜今天吃什麼,先來這裡看看
代碼片段

Git 出錯了怎麼辦(急救)

rebase 衝突、提交錯分支、誤刪分支的補救方法。

範本正文

# 提交到了錯誤的分支(還沒 push)
git branch feat/right          # 先在新分支上留下這個提交
git reset --hard HEAD~1        # 再把這個分支回退一步
git switch feat/right

# rebase 中途想放棄
git rebase --abort

# 衝突解決完繼續
git add <file> && git rebase --continue

# 誤刪了分支
git reflog                     # 找到那次的 commit hash
git switch -c <branch> <hash>  # 用 hash 把分支恢復出來

# 臨時保存手頭的改動
git stash push -m "半成品"
git stash pop

怎麼用

出事時按情況查,先別慌著刪倉庫。

使用要點

  • reflog 是你的後悔藥,只要沒被 GC 掉,幾乎什麼都能找回來。
  • 衝突時先看 git status 的提示,它會告訴你下一步該做什麼。

同類範本

Git 出錯了怎麼辦(急救) · 閱界食譜