首頁·終端機命令

git rebase

git

用法

git rebase [-i] [--onto <base>] <upstream>

把你的提交重新放到另一個基點上;雜湊會變,所以別對別人已經拉走的分支做 rebase,那種情況用 merge。

常用選項

選項含義
-iinteractive: reorder, squash, reword, drop commits
--onto <base>move the range onto a different base
--continuecarry on after fixing a conflict
--abortput the branch back exactly as it was
--skipdrop the commit that will not apply
--autostashstash uncommitted work and put it back afterwards
--autosquashapply fixup!/squash! commits automatically

範例

git rebase main

replays your commits on top of main, giving them new hashes

git rebase -i HEAD~3

squash or reword the last three commits

git rebase --abort

cancel a rebase and return to the original branch

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

怎麼看

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

常見問題

Q. git rebase 是做什麼的?

把你的提交重新放到另一個基點上;雜湊會變,所以別對別人已經拉走的分支做 rebase,那種情況用 merge。

Q. 怎麼寫?

git rebase [-i] [--onto <base>] <upstream> —— 方括號表示可以省略的部分。

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

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

相關命令

man 手冊: man git-rebase