Done

用法一: git rebase —onto

Before After
image.png image.png

方法一:

  1. // 切换到 F 提交
  2. git checkout F
  3. //把"范围"类的提交变基到 E 上面去
  4. git rebase E
  5. // "范围" 就是 E..F, 根据开闭原则. 也就等于 F
  6. // p结果就是把 F 变基到了 E 后面

方法二:

  1. // 使用 "--onto" 参数指定变基后的基点, 也就是 E
  2. git checkout F
  3. git rebase --onto=E F^
  4. // 根据开闭原则,需要指定 F^..F=C..F=F

变基

image.png

  1. // 需要变更 test 分支
  2. git checkout test
  3. // dev分支为基准进行变基
  4. git rebase dev
  5. // 如果发生冲突
  6. // 可以执行多次 git rebase --skip, 跳过补丁1',2' 最后test分支保留了dev分支的修改,丢掉了12两个提交.
  7. // 也可以执行 git rebase --abort 放弃更改,也就是checkout原始test分支,保留了12两个提交.
  8. // 没有冲突,或者解决冲突后执行 git rebase --continue ,变基成功,test 分支指向2'.
  9. // 然后Fast-Forward
  10. git checkout dev
  11. git 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)

Git Rebase 详解 - 图4

需求一: 合并 merge 提交,使主分支变成一条直线 (To remove the merge commit and squash the branch into a single commit in the mainline)

Git Rebase 详解 - 图5
执行命令:

  1. //Use these commands
  2. // (replacing 5 and 1 with the SHAs of the corresponding commits):
  3. git checkout 5
  4. git reset --soft 1
  5. git commit --amend -m '1 2 3 4 5'
  6. git rebase HEAD master

需求二: 保留 merge 提交,合并分支提交 (To retain a merge commit but squash the branch commits into one:)

Git Rebase 详解 - 图6
执行命令:

  1. //Use these commands
  2. // (replacing 5, 1 and C with the SHAs of the corresponding commits):
  3. git checkout -b tempbranch 5
  4. git reset --soft 1
  5. git commit --amend -m '1 2 3 4 5'
  6. git checkout C
  7. git merge --no-ff tempbranch
  8. git rebase HEAD master

需求三: 合并 merge 提交, 保留所有提交历史, 并且使主分支变成一条直线(To remove the merge commit and replace it with individual commits from the branch)

Git Rebase 详解 - 图7

执行命令:

  1. //Just do (replacing 5 with the SHA of the corresponding commit):
  2. git rebase 5 master

如果想得到 “A B C 1 2 3 4 5 D …” 这样的顺序, 执行:

  1. git rebase C 5
  2. git rebase 5 master

需求四: 删除分支 (And finally, to remove the branch entirely)

Git Rebase 详解 - 图8
执行命令:

  1. // Use this command
  2. // (replacing C and D with the SHAs of the corresponding commits):
  3. git rebase --onto C D~ master