合并commit的几种方式调研

488 阅读1分钟

合并commit的几种方式

  • 仅合并代码时候压缩commit信息
git merge <your branch> --squash

相当于线上操作发起merge-request时勾选squash选项

  • 使用 reset
git reset --soft <before-first-commit-id>
git push -f

注意:

  1. reset是前开后闭的区间
  2. 此种方式合代码会有问题,会算作一个全新提交,已经合并过此分支的代码会有问题(有冲突解决冲突,无冲突的更改文件无法追踪,不建议使用)
  • 使用 rebase
git rebase -i <before-first-commit-id>

pick 改成 squash,往上压缩,最后填写commit信息

暂测无问题,需要解决冲突。


综上,最安全和简单的方式是git merge --squash,在合并时压缩commit信息,同时可以做到保留原有的commit历史