- 用法一: git rebase —onto
- 变基
- 变更提交历史
- emove merge commit from history)">清理历史记录里面的merge提交 (Remove merge commit from history)
- 初始状态 (Starting with the repo in the original state)
- 需求一: 合并 merge 提交,使主分支变成一条直线 (To remove the merge commit and squash the branch into a single commit in the mainline)
- 需求二: 保留 merge 提交,合并分支提交 (To retain a merge commit but squash the branch commits into one:)
- 需求三: 合并 merge 提交, 保留所有提交历史, 并且使主分支变成一条直线(To remove the merge commit and replace it with individual commits from the branch)
- 需求四: 删除分支 (And finally, to remove the branch entirely)
用法一: git rebase —onto
| Before | After |
|---|---|
![]() |
![]() |
方法一:
// 切换到 F 提交git checkout F//把"范围"类的提交变基到 E 上面去git rebase E// "范围" 就是 E..F, 根据开闭原则. 也就等于 F// p结果就是把 F 变基到了 E 后面
方法二:
// 使用 "--onto" 参数指定变基后的基点, 也就是 Egit checkout Fgit rebase --onto=E F^// 根据开闭原则,需要指定 F^..F=C..F=F
变基

// 需要变更 test 分支git checkout test// 以dev分支为基准进行变基git rebase dev// 如果发生冲突// 可以执行多次 git rebase --skip, 跳过补丁1',2'。 最后test分支保留了dev分支的修改,丢掉了1,2两个提交.// 也可以执行 git rebase --abort 放弃更改,也就是checkout原始test分支,保留了1,2两个提交.// 没有冲突,或者解决冲突后执行 git rebase --continue ,变基成功,test 分支指向2'.// 然后Fast-Forwardgit checkout devgit merge test
变更提交历史
git reabse -i HEAD~3
// 1 发现冲突
// 2 解决冲突 ,git add
// 3 git rebase —continue , 修改 message
// 重复 1~3
清理历史记录里面的merge提交 (Remove merge commit from history)
https://stackoverflow.com/questions/17577409/git-remove-merge-commit-from-history
初始状态 (Starting with the repo in the original state)
需求一: 合并 merge 提交,使主分支变成一条直线 (To remove the merge commit and squash the branch into a single commit in the mainline)
//Use these commands// (replacing 5 and 1 with the SHAs of the corresponding commits):git checkout 5git reset --soft 1git commit --amend -m '1 2 3 4 5'git rebase HEAD master
需求二: 保留 merge 提交,合并分支提交 (To retain a merge commit but squash the branch commits into one:)
//Use these commands// (replacing 5, 1 and C with the SHAs of the corresponding commits):git checkout -b tempbranch 5git reset --soft 1git commit --amend -m '1 2 3 4 5'git checkout Cgit merge --no-ff tempbranchgit rebase HEAD master
需求三: 合并 merge 提交, 保留所有提交历史, 并且使主分支变成一条直线(To remove the merge commit and replace it with individual commits from the branch)
执行命令:
//Just do (replacing 5 with the SHA of the corresponding commit):git rebase 5 master
如果想得到 “A B C 1 2 3 4 5 D …” 这样的顺序, 执行:
git rebase C 5git rebase 5 master
需求四: 删除分支 (And finally, to remove the branch entirely)
// Use this command// (replacing C and D with the SHAs of the corresponding commits):git rebase --onto C D~ master







