Rebase 的使用

git rebase 用于将一个分支的提交迁移到另一个分支之后,保持提交历史线性清晰。

基本用法

切换到要变基的分支,然后执行:

git rebase <目标分支>

例如,将 feature 分支变基到 main

git checkout feature
git rebase main

Git 会将 feature 分支的提交“移动”到 main 最新提交之后。

常见用途

  • 清理杂乱的提交历史
  • 避免使用 merge 产生多条分支线
  • 在推送前整理自己的开发记录

冲突处理

如发生冲突,Git 会中止变基,需手动解决冲突并继续:

git add .
git rebase --continue

如需放弃变基操作:

git rebase --abort

rebase 是强大的工具,但使用时需确保不会覆盖他人已推送的提交。