今天看到一篇文章, 讲到了 git rebase 与 git merge 的区别. 我觉得讲的非常好.
功能区别
背景
比如一个git项目有两个分支, master 和 feature. 当前在feature 开发. 那 feature 是从 master fork 出来的.
这个时候, 别人也在向 master 合并代码. 那么现在 master 和 feature 已经分叉了. 这时, 如果想让master的新提交在feature也出现.
有两个方法, 一是git rebase, 二是git merge.
- git merge. 会把 master 的内容和feature的内容合并, 并产生一个新的 commit.
- git rebase. 会把当前feautre 分支到master的根commit到最新的commit都修改一遍. 使feature与master的分叉commit 建立在 master 的最新commit 之后. 这样产生的历史是线性的.
git rebase 的注意事项
git rebase 应该永远使用在私有分支上. 因为它会修改 commit, 如果是公开的分支, 会影响别人.
上面没有图, 只有文字说明, 比较抽象. 具体详细的说明参见: https://www.atlassian.com/git/tutorials/merging-vs-rebasing