git rebase 与 git reset的应用

常用的应用场景

  1. 合并提交

  2. 去除不要的提交

实际应用

场景1,合并多个提交

有 A B C D E F 6个提交,当前分支develop的最新提交为F,C D E F都是某一功能的持续提交,现需要把C到F的commit合并成一个commit,去除多余提交

  1. 方法一: git reset
git reset B --soft
git ci -m "merge C to F"
  1. 方法二: git rebase
git rebase --onto B C^ develop

场景2,去掉D提交

此时,git reset已经不再适合这个场景

使用git rebase

git rebase --onto C E^ develop

后记

  1. git rebase 语法

git rebase --onto <onbase> <since> <till>
git rebase --onto <onbase> <since>
git rebase <since> <till>
git rebase <since>
git rebase -i

onbase为要架接到的commit, since表示不包括自身到till的所有提交

git rebase --continue # 解决冲突后使用,以完成rebase
git rebase --skip # 跳过当前commit,进行下一个commit的合并
git rebase --abort # 放弃此次操作,还原工作区

  1. git rebase的莫名冲突

对于场景二: 使用git rebase可能出现冲突(感觉不应该出现冲突的,对此不是很理解,难道是因为丢弃D后使查找链C-D-E-F断裂造成git认为这两个commit需要合并,是因为E无法知道C是它的祖先吗)

在解决冲突后,使用git rebase --continue完成rebase操作