git rebase 命令 简介 git rebase 是 Git 中一个强大的命令,用于重新应用提交。它可以将一个分支的提交”重新播放”到另一个分支上,从而创建一条线性的提交历史。与 git merge 不同,rebase 会重写提交历史,使历史记录更加清晰和线性。
基本概念 什么是 Rebase Rebase 的意思是”变基”,它会:
找到当前分支和目标分支的共同祖先
将当前分支的提交”撤销”(临时保存)
将当前分支指向目标分支的最新提交
重新应用之前保存的提交
Rebase vs Merge
特性
Rebase
Merge
历史记录
线性、清晰
保留分支结构
提交历史
重写提交哈希
保留原始提交
合并提交
不创建合并提交
创建合并提交
适用场景
功能分支合并到主分支
需要保留分支历史
风险
重写历史,需谨慎
安全,不改变历史
冲突处理
逐个提交解决
一次性解决
何时使用 Rebase ✅ 适合使用 Rebase :
功能分支合并到主分支前,保持历史线性
清理提交历史(交互式 rebase)
同步远程分支的更新
❌ 不适合使用 Rebase :
已经推送到公共分支的提交
多人协作的共享分支
需要保留完整分支历史的情况
命令语法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 git rebase [选项] <目标分支> git rebase -i [选项] <目标分支> git rebase --continue git rebase --abort git rebase --skip
查看帮助文档
常用选项
选项
说明
-i, --interactive
交互式 rebase,可以编辑、删除、合并提交
--continue
解决冲突后继续 rebase
--abort
中止 rebase,回到 rebase 前的状态
--skip
跳过当前提交(通常用于空提交)
--onto <newbase>
将提交重新应用到新的基础上
-p, --preserve-merges
保留合并提交(已废弃,使用 –rebase-merges)
--rebase-merges
保留并重新创建合并提交
--autosquash
自动压缩标记为 fixup/squash 的提交
--autostash
自动暂存未提交的更改
-v, --verbose
详细输出
-q, --quiet
静默模式
--no-verify
跳过 pre-rebase 钩子
--exec <cmd>
在每个提交后执行命令
--root
从根提交开始 rebase
--strategy=<strategy>
使用指定的合并策略
-X, --strategy-option=<option>
传递选项给合并策略
基本使用 1. 基本 Rebase 将当前分支 rebase 到目标分支 1 2 3 4 5 $ git checkout feature-branch $ git rebase main
工作流程 :
Git 找到 feature-branch 和 main 的共同祖先
临时保存 feature-branch 的提交
将 feature-branch 指向 main 的最新提交
重新应用保存的提交
示例 1 2 3 4 5 6 7 8 9 10 11 12 13 main: A---B---C \ feature: D---E $ git checkout feature $ git rebase main main: A---B---C \ feature: D'---E'
2. 交互式 Rebase 交互式 rebase 允许你编辑、删除、合并、重新排序提交。
基本用法 1 2 3 4 5 6 7 8 $ git rebase -i HEAD~3 $ git rebase -i main $ git rebase -i abc1234
交互式编辑器 打开编辑器后,会看到类似这样的内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 pick abc1234 First commit pick def5678 Second commit pick ghi9012 Third commit # Rebase abc1234..ghi9012 onto xyz3456 (3 commands) # # Commands: # p, pick <commit> = use commit # r, reword <commit> = use commit, but edit the commit message # e, edit <commit> = use commit, but stop for amending # s, squash <commit> = use commit, but meld into previous commit # f, fixup <commit> = like "squash", but discard this commit's log message # x, exec <command> = run command (the rest of the line) using shell # b, break = stop here (continue rebase with 'git rebase --continue') # d, drop <commit> = remove commit # l, label <label> = label current HEAD with a name # t, reset <label> = reset HEAD to the label # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>] # . create a merge commit using the original merge commit's # . message (or the oneline, if no original merge message was # . specified). Use -c <commit> to reword the commit message. # # These lines can be re-ordered; they are executed from top to bottom.
交互式命令说明
命令
缩写
说明
pick
p
使用该提交(不做修改)
reword
r
使用提交,但修改提交信息
edit
e
使用提交,但暂停以便修改
squash
s
使用提交,但合并到前一个提交
fixup
f
类似 squash,但丢弃提交信息
exec
x
执行 shell 命令
drop
d
删除提交
break
b
在此处暂停
3. 处理 Rebase 冲突 冲突发生 1 2 3 4 5 6 7 8 $ git rebase main Auto-merging file.txt CONFLICT (content): Merge conflict in file.txt error: could not apply abc1234... Commit message $ git add file.txt $ git rebase --continue
解决冲突的步骤 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 $ git status $ vim conflicted-file.txt $ git add conflicted-file.txt $ git rebase --continue $ git rebase --skip $ git rebase --abort
4. 中止和继续 Rebase 1 2 3 4 5 6 7 8 $ git rebase --abort $ git rebase --continue $ git rebase --skip
高级用法 1. 使用 –onto 重新应用提交 1 2 3 4 5 $ git rebase --onto new-base old-base feature $ git rebase --onto main feature~3 feature
2. 保留合并提交 1 2 3 4 5 $ git rebase --rebase-merges main $ git rebase -p main
3. 自动压缩提交 1 2 3 4 5 6 $ git rebase -i --autosquash HEAD~5 $ git commit --fixup abc1234 $ git rebase -i --autosquash abc1234^
4. 自动暂存未提交的更改 1 2 $ git rebase --autostash main
5. 在每个提交后执行命令 1 2 3 4 5 $ git rebase -i --exec "npm test" HEAD~5 $ git rebase -i --exec "npm run lint" main
6. 从根提交开始 Rebase
实际应用场景 场景1:同步主分支的更新 1 2 3 4 5 6 7 $ git checkout feature-branch $ git fetch origin $ git rebase origin/main $ git pull --rebase origin main
场景2:清理提交历史 1 2 3 4 5 6 7 8 $ git rebase -i HEAD~5
场景3:修改提交信息 1 2 3 4 5 6 7 $ git rebase -i HEAD~1 $ git rebase -i HEAD~3
场景4:合并多个提交 1 2 3 4 5 6 7 8 9 10 11 $ git rebase -i HEAD~5 pick abc1234 Important feature squash def5678 Fix typo squash ghi9012 Update docs squash jkl3456 Fix test pick mno7890 Another feature
场景5:删除提交 1 2 3 4 5 6 7 $ git rebase -i HEAD~5 pick abc1234 Commit 1 drop def5678 Commit 2 pick ghi9012 Commit 3
场景6:重新排序提交 1 2 3 4 5 6 7 $ git rebase -i HEAD~5 pick ghi9012 Commit 3 pick abc1234 Commit 1 pick def5678 Commit 2
场景7:拆分提交 1 2 3 4 5 6 7 8 9 10 11 12 13 $ git rebase -i HEAD~3 edit abc1234 Large commit $ git reset HEAD~1 $ git add file1.txt $ git commit -m "First part" $ git add file2.txt $ git commit -m "Second part" $ git rebase --continue
场景8:修复之前的提交 1 2 3 4 5 $ git commit --fixup abc1234 $ git rebase -i --autosquash abc1234^
场景9:同步远程分支 1 2 3 4 5 6 $ git pull --rebase origin main $ git fetch origin $ git rebase origin/main
场景10:将功能分支 rebase 到主分支 1 2 3 4 5 6 7 8 9 10 11 12 $ git checkout main $ git pull origin main $ git checkout feature-branch $ git rebase main $ git add . $ git rebase --continue $ git push --force-with-lease origin feature-branch
与 Merge 的对比 Merge 方式 1 2 3 $ git checkout main $ git merge feature-branch
历史记录 :
1 2 3 main: A---B---C-------M \ / feature: D---E-----/
Rebase 方式 1 2 3 4 5 $ git checkout feature-branch $ git rebase main $ git checkout main $ git merge feature-branch
历史记录 :
1 main: A---B---C---D'---E'
选择建议
使用 Merge :需要保留分支历史、多人协作、已推送的提交
使用 Rebase :个人功能分支、清理历史、保持线性历史
常见问题和解决方案 问题1:Rebase 冲突 问题 :rebase 过程中出现冲突
解决方案 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 $ git status $ vim conflicted-file.txt $ git add conflicted-file.txt $ git rebase --continue $ git rebase --abort
问题2:已经推送的提交被 rebase 问题 :rebase 改变了提交历史,需要强制推送
解决方案 :
1 2 3 4 5 $ git push --force-with-lease origin branch-name $ git push --force origin branch-name
警告 :不要对共享分支使用 force push!
问题3:Rebase 过程中想放弃 解决方案 :
问题4:交互式 rebase 编辑器不熟悉 问题 :不知道如何使用 vim/emacs
解决方案 :
1 2 3 4 $ git config --global core.editor "code --wait" $ git config --global core.editor "nano" $ git config --global core.editor "vim"
问题5:Rebase 后提交哈希改变 问题 :这是正常现象,rebase 会重写提交历史
说明 :
Rebase 会创建新的提交(新的哈希值)
原始提交仍然存在(通过 reflog 可以找回)
这是 rebase 的预期行为
问题6:Rebase 后丢失提交 问题 :rebase 过程中误操作
解决方案 :
1 2 3 4 5 6 7 8 $ git reflog $ git checkout abc1234 $ git checkout -b recovery-branch
问题7:Rebase 太多次提交很慢 问题 :rebase 大量提交时很慢
解决方案 :
1 2 3 4 5 $ git rebase --strategy-option=ours main $ git rebase -i HEAD~10
最佳实践 1. 只对本地提交使用 Rebase 1 2 3 4 5 $ git rebase main $ git rebase main
2. 使用 –force-with-lease 而不是 –force 1 2 3 4 5 $ git push --force-with-lease origin branch-name $ git push --force origin branch-name
3. 定期同步主分支 1 2 3 $ git fetch origin $ git rebase origin/main
4. 使用交互式 rebase 清理提交 1 2 3 4 5 6 $ git rebase -i HEAD~5
5. 解决冲突后仔细检查 1 2 3 4 5 6 7 $ git add . $ git rebase --continue $ git log --oneline $ git status
6. 使用 –autostash 自动暂存 1 2 $ git rebase --autostash main
7. 团队协作时沟通
8. 使用 –rebase-merges 保留合并 1 2 $ git rebase --rebase-merges main
9. 配置 pull.rebase 1 2 3 4 5 $ git config --global pull.rebase true $ git pull
10. 备份重要分支 1 2 3 $ git branch backup-branch $ git rebase main
常用工作流 工作流1:功能分支开发 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 $ git checkout -b feature/new-feature main $ git add . $ git commit -m "Add feature" $ git fetch origin $ git rebase origin/main $ git push origin feature/new-feature
工作流2:清理提交历史 1 2 3 4 5 6 7 8 9 10 11 12 13 $ git log --oneline $ git rebase -i HEAD~10 $ git push --force-with-lease origin branch-name
工作流3:修复之前的提交 1 2 3 4 5 6 7 8 $ git commit --fixup abc1234 $ git rebase -i --autosquash abc1234^ $ git push --force-with-lease origin branch-name
安全注意事项 ⚠️ 重要警告
不要 rebase 已推送的公共分支
会重写历史,影响其他开发者
如果必须,先与团队沟通
使用 –force-with-lease 而不是 –force
--force-with-lease 更安全
会检查远程是否有新的提交
Rebase 前备份重要分支
1 $ git branch backup-branch
解决冲突后仔细检查
团队协作时沟通
如果需要对共享分支 rebase,先通知团队
确保其他人知道历史会被重写
总结 git rebase 是一个强大的命令,可以帮助你:
保持线性历史 :创建清晰、线性的提交历史
清理提交 :合并、删除、重新排序提交
同步更新 :将功能分支同步到主分支的最新状态
修改历史 :编辑提交信息、拆分提交
关键要点 :
✅ 只对本地提交使用 rebase
✅ 使用 --force-with-lease 而不是 --force
✅ 定期同步主分支更新
✅ 解决冲突后仔细检查
❌ 不要 rebase 已推送的公共分支
❌ 团队协作时先沟通
通过合理使用 git rebase,可以创建更清晰、更易维护的 Git 历史记录。