• 7. 用rebase合并

    教程1 操作分支

    7. 用rebase合并

    合并issue3分支的时候,使用rebase可以使提交的历史记录显得更简洁。

    现在暂时取消刚才的合并。

    1. $ git reset --hard HEAD~

    rebase前的历史记录

    切换到issue3分支后,对master执行rebase。

    1. $ git checkout issue3
    2. Switched to branch 'issue3'
    3. $ git rebase master
    4. First, rewinding head to replay your work on top of it...
    5. Applying: 添加pull的说明
    6. Using index info to reconstruct a base tree...
    7. <stdin>:13: new blank line at EOF.
    8. +
    9. warning: 1 line adds whitespace errors.
    10. Falling back to patching base and 3-way merge...
    11. Auto-merging myfile.txt
    12. CONFLICT (content): Merge conflict in myfile.txt
    13. Failed to merge in the changes.
    14. Patch failed at 0001 添加pull的说明
    15.  
    16. When you have resolved this problem run "git rebase --continue".
    17. If you would prefer to skip this patch, instead run "git rebase --skip".
    18. To check out the original branch and stop rebasing run "git rebase --abort".

    和merge时的操作相同,修改在myfile.txt发生冲突的部分。

    1. 连猴子都懂的Git命令
    2. add 把变更录入到索引中
    3. <<<<<<< HEAD
    4. commit 记录索引的状态
    5. =======
    6. pull 取得远端数据库的内容
    7. >>>>>>> issue3

    rebase的时候,修改冲突后的提交不是使用commit命令,而是执行rebase命令指定 —continue选项。若要取消rebase,指定 —abort选项。

    1. $ git add myfile.txt
    2. $ git rebase --continue
    3. Applying: 添加pull的说明

    目前的历史记录

    这样,在master分支的issue3分支就可以fast-forward合并了。切换到master分支后执行合并。

    1. $ git checkout master
    2. Switched to branch 'master'
    3. $ git merge issue3
    4. Updating 8f7aa27..96a0ff0
    5. Fast-forward
    6. myfile.txt | 1 +
    7. 1 files changed, 1 insertions(+), 0 deletions(-)

    myfile.txt的最终内容和merge是一样的,但是历史记录如下。

    目前的历史记录

    前一页

    下一页