git rebase 是什么?git rebase 是一种 Git 命令,用于将一个分支的更改重新应用到另一个分支的基础上。它的核心是将当前分支的提交历史移动到目标分支的最新状态之上,生成一个更线性的历史记录。
与 git merge 不同,rebase 不会创建合并提交,而是重写历史。
git checkout <要更新的分支>
git rebase <目标分支>
<目标分支> 的最新提交作为基础,把 <要更新的分支> 的提交逐一应用上去。git rebase比如有原来有3个分支,1分支logo是小明的,2分支的logo是小红的,3分支的logo是小刚的;但是他们的功能都是一样的,只是logo不同,但是代码修复了,每个人都需要更新最新的代码,保留自己的logo。
所以:
branch-xiaoming、branch-xiaohong、branch-xiaogang),功能代码相同,仅 logo 不同。logo。假设有一个核心分支 core 包含共享代码,修复也在 core 上完成,我们可以用 git rebase 将修复应用到个性化分支。
初始化分支
core 分支包含共享代码(无 logo):git checkout -b core
echo "Shared code" > core-function.js
git add core-function.js
git commit -m "Initial core code"
git push origin core
logo:git checkout core
git branch branch-xiaoming
git checkout branch-xiaoming
cp xiaoming-logo.png logo.png
git add logo.png
git commit -m "Add Xiaoming's logo"
git push origin branch-xiaoming
(为小红、小刚类似操作)在 core 上修复代码
git checkout core
echo "Bug fix" >> core-function.js
git add core-function.js
git commit -m "Fix bug in core function"
git push origin core
用 rebase 更新个性化分支
branch-xiaoming:git checkout branch-xiaoming
git rebase core
branch-xiaoming 和 core 的共同祖先。core 的新提交("Fix bug in core function")作为新基础。branch-xiaoming 的提交("Add Xiaoming's logo")重新应用上去。git push origin branch-xiaoming --force
(因为 rebase 重写了历史,需要用 --force 推送)branch-xiaohong 和 branch-xiaogang 重复以上操作。结果
core: Initial core code -> Fix bug in core function
branch-xiaoming: Initial core code -> Fix bug in core function -> Add Xiaoming's logo
git rebase 的优点git merge 的合并提交,rebase 让提交历史更简洁,像是一条直线。* Add Xiaoming's logo
* Fix bug in core function
* Initial core code
core 的修复应用到个性化分支,避免额外的合并节点。logo 文件只在个性化分支上有提交,rebase 不会覆盖它们,除非 core 修改了同一个文件(这时会有冲突)。git rebase 的缺点与注意事项core 的修复和个性化分支的改动冲突(比如都改了 core-function.js),需要手动解决:git rebase core
# 冲突发生,解决后
git add <冲突文件>
git rebase --continue
rebase 改变了提交历史,已推送的分支需要用 git push --force,可能影响协作(如果多人共用分支)。merge 的直观性,rebase 对新手不友好,误操作可能导致历史混乱。| 方案 | 优点 | 缺点 | 适用性 |
|---|---|---|---|
| cherry-pick | 简单,选择性应用特定提交 | 频繁更新时手动操作多 | 小规模、临时更新 |
| merge | 直观,保留历史,协作友好 | 历史有合并节点,可能不简洁 | 长期维护、团队协作 |
| subtree | 核心代码独立,隔离性强 | 设置复杂,需额外仓库 | 模块化管理 |
| rebase | 线性历史,干净同步 | 重写历史,冲突需手动处理 | 追求简洁历史、单人维护 |
merge vs rebase:merge:会在每个分支上多一个合并提交("Merge branch 'core' into branch-xiaoming"),历史更复杂但更安全。rebase:历史线性,像所有提交都发生在同一条线上,但需要 --force 推送。rebase 是个好选择。merge 更稳妥。git rebase结合你的问题,我建议用 rebase 的方案如下:
# 确保 core 分支最新
git checkout core
git pull origin core
# 修复代码
echo "Bug fix" >> core-function.js
git add core-function.js
git commit -m "Fix bug"
git push origin core
# 更新小明分支
git checkout branch-xiaoming
git rebase core
git push origin branch-xiaoming --force
# 更新小红分支
git checkout branch-xiaohong
git rebase core
git push origin branch-xiaohong --force
# 更新小刚分支
git checkout branch-xiaogang
git rebase core
git push origin branch-xiaogang --force
若 core-function.js 在个性化分支也有改动:
git rebase core
# 冲突发生,编辑文件保留 logo 和修复
git add core-function.js
git rebase --continue
git push origin branch-xiaoming --force
rebase:它能保持历史线性,适合你描述的场景(功能相同、仅 logo 不同),每次更新就像在各自分支上“续写”核心代码。--force 推送时,确保团队知晓(若有协作)。merge 更简单。