Reference Git reference

git rebase 参考

git rebase 会把一组提交移动到新的基底上。它常用于让分支历史更线性,也常用 git rebase -i 整理本地提交。

语法

bash
git rebase <base>
git rebase -i <base>

常用命令

把当前分支变基到 main:

bash
git fetch origin
git rebase origin/main

交互式整理最近 3 个提交:

bash
git rebase -i HEAD~3

冲突处理:

bash
git status
git add .
git rebase --continue

放弃 rebase:

bash
git rebase --abort

常见错误

  • 对已经多人共享的分支随意 rebase,会改写历史并影响协作者。
  • rebase 冲突时没有逐个提交理解冲突来源,容易把改动合错。
  • rebase 后需要推送时直接 --force,更安全的选择通常是 --force-with-lease。

相关内容