Back

git - rebase 笔记 ( git rebase introduction)

发布时间: 2013-05-27 02:56:00

前几天的一个文章,我提到 自己比较偏好 git-merge. 现在看起来,这个问题不该带有个人偏好。而是需要根据情况,来选择使用 git rebase /merge  ( several days ago, I mentioned that I prefer 'git-merge' to 'git-rebase'.  Now I admit that as a professional programmer, I should not be emotional.  We should choosing the right one according to the real situation ) 

merge: 会保留时间线。 适合需要保持时间线的场合。  (it would keep the commit order for each branch ) 

rebase: 不会保留时间线。 适合按照 feature 来查看log的场合。比如,我现在同时在3个分支上工作,但是为了方便代码的提交,我会在一个完成之后再提交另外一个分支/功能模块。所以在这样的情况下使用rebase使得代码更加容易回滚,以及查看log.   ( the origin commit would be removed and a new commit would be generated .  if you are working on 3 branches at the same time, and want to merge the code till the whole module/branch is done, just use this 'rebase' ) 

简单用法:

$ git rebase master <current_branch>

这样就会把 master上的最后一个commit, 做为当前 分支的commit 的基础。

更多详细,参考 man git-rebase.   ( for more details, please refer to 'man git-rebase' )

用法:

1. git checkout <当前工作分支>

2. git merge master (把master上的最新代码合并到当前分支)  (可能有冲突,要合并,)

3. git rebase master (把当前分支上的代码的base 变为 master上的最后一个)   (可能要冲突,要合并)

然后就根据git status的各种提示来操作,有冲突就合并冲突。

记得上面步骤2,3的合并时,一定要看好,一个是HEAD在上面, 一个是 正确的代码在上面。如下图所示:

Back