Appearance
变基操作
概述
变基(Rebase)是 Git 中整合分支修改的另一种方式。与合并不同,变基会重写提交历史,使其更加线性整洁。
什么是变基
变基原理
变基是将一系列提交移动到一个新的基础提交上的过程。
# 变基前
A --- B --- C (main)
\
D --- E (feature)
# 变基后
A --- B --- C (main)
\
D' --- E' (feature)变基 vs 合并
# 合并:保留分支历史,创建合并提交
A --- B --- C --- M (main)
\ /
D --- E (feature)
# 变基:重写历史,线性提交
A --- B --- C --- D' --- E' (main, feature)git rebase 基本用法
基本语法
bash
# 将当前分支变基到指定分支
git rebase <base-branch>
# 将当前分支变基到指定提交
git rebase <commit>
# 将指定分支变基到另一个分支
git rebase <base-branch> <topic-branch>变基流程
bash
# 1. 切换到功能分支
git checkout feature
# 2. 变基到 main 分支
git rebase main
# 输出:
# First, rewinding head to replay your work on top of it...
# Applying: Add feature A
# Applying: Add feature B变基过程
bash
# 变基时的操作步骤
# 1. 找到两个分支的共同祖先
# 2. 保存当前分支的提交
# 3. 将当前分支重置到目标分支
# 4. 逐个应用保存的提交处理变基冲突
冲突发生
bash
git rebase main
# 输出:
# Auto-merging file.txt
# CONFLICT (content): Merge conflict in file.txt
# error: could not apply a1b2c3d... Add feature
# Resolve all conflicts manually, mark them as resolved with
# "git add/rm <conflicted_files>", then run "git rebase --continue".解决冲突
bash
# 1. 查看冲突文件
git status
# 2. 编辑冲突文件,解决冲突
vim file.txt
# 3. 标记冲突已解决
git add file.txt
# 4. 继续变基
git rebase --continue变基选项
bash
# 跳过当前提交
git rebase --skip
# 中止变基,恢复到变基前状态
git rebase --abort
# 使用特定版本解决冲突
git checkout --ours file.txt # 使用当前版本
git checkout --theirs file.txt # 使用目标分支版本交互式变基
交互式变基允许你在变基过程中修改提交历史。
启动交互式变基
bash
# 交互式变基最近 3 个提交
git rebase -i HEAD~3
# 交互式变基到指定提交
git rebase -i <commit>
# 交互式变基到指定分支
git rebase -i main编辑器界面
pick a1b2c3d Add feature A
pick b2c3d4e Add feature B
pick c3d4e5f Fix bug in feature B
# Rebase instructions:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit常用操作
修改提交信息
# 将 pick 改为 reword
reword a1b2c3d Add feature A
pick b2c3d4e Add feature B
pick c3d4e5f Fix bug in feature B合并提交
# 将多个提交合并为一个
pick a1b2c3d Add feature A
squash b2c3d4e Add feature B
squash c3d4e5f Fix bug in feature B
# 保存后会提示编辑合并后的提交信息编辑提交
# 在指定提交处暂停,允许修改
pick a1b2c3d Add feature A
edit b2c3d4e Add feature B
pick c3d4e5f Fix bug in feature B
# 变基会在 edit 提交处暂停
# 修改完成后
git add .
git commit --amend
git rebase --continue删除提交
# 删除不需要的提交
pick a1b2c3d Add feature A
drop b2c3d4e Add feature B
pick c3d4e5f Fix bug in feature B
# 或直接删除那一行
pick a1b2c3d Add feature A
pick c3d4e5f Fix bug in feature B重新排序提交
# 调整提交顺序
pick c3d4e5f Fix bug in feature B
pick a1b2c3d Add feature A
pick b2c3d4e Add feature B高级变基技巧
自动压缩提交
bash
# 配置自动压缩
git config --global rebase.autoSquash true
# 创建标记为压缩的提交
git commit --fixup a1b2c3d
git commit --squash b2c3d4e
# 变基时自动压缩
git rebase -i --autosquash main变基到特定提交
bash
# 变基到特定提交
git rebase --onto main a1b2c3d feature
# 将 feature 分支从 a1b2c3d 之后的所有提交变基到 main
# 示例场景
# main: A --- B --- C
# feature: A --- D --- E --- F
# 执行:git rebase --onto main A feature
# 结果:main: A --- B --- C --- D' --- E' --- F'保留合并提交
bash
# 变基时保留合并提交
git rebase -p main
# 或
git rebase --preserve-merges main变基后同步远程
bash
# 变基后需要强制推送
git push --force origin feature
# 更安全的方式:使用 --force-with-lease
git push --force-with-lease origin feature变基的风险
不要在公共分支上变基
bash
# 危险操作!
# 不要在已推送到远程的分支上变基
# 错误示例
git checkout main
git rebase feature # main 是公共分支
# 正确做法
git checkout feature
git rebase main # 在功能分支上变基变基会改变提交哈希
bash
# 变基前的提交
a1b2c3d Add feature A
b2c3d4e Add feature B
# 变基后的提交(哈希已改变)
e5f6g7h Add feature A
f6g7h8i Add feature B
# 如果其他人已经基于旧提交工作,会造成问题团队协作注意事项
bash
# 规则:只在自己的分支上变基
# 已推送的分支变基后,需要团队协调
# 如果必须变基公共分支
# 1. 通知所有团队成员
# 2. 确保所有人都完成当前工作
# 3. 变基后,所有人需要重新克隆或重置变基工作流
功能分支变基
bash
# 1. 创建功能分支
git checkout -b feature main
# 2. 开发并提交
git add .
git commit -m "Add feature"
# 3. 定期同步主分支
git fetch origin
git rebase origin/main
# 4. 解决冲突(如果有)
git add .
git rebase --continue
# 5. 推送到远程
git push -u origin feature
# 6. 后续同步后强制推送
git push --force-with-lease origin feature清理提交历史
bash
# 合并前清理功能分支
git checkout feature
# 交互式变基,整理提交
git rebase -i main
# 常见操作:
# - 合并小提交
# - 修改提交信息
# - 删除临时提交
# - 重新排序提交
# 推送清理后的分支
git push --force-with-lease origin feature总结
- 变基可以创建更整洁的提交历史
- 交互式变基允许修改提交历史
- 不要在公共分支上变基
- 变基后需要强制推送
- 使用
--force-with-lease更安全
下一步
学习完变基操作后,建议继续学习:
- Merge vs Rebase - 比较两种整合方式
- 重置提交 - 学习更多历史操作
