带有多个子分支的git rebase

时间:2017-06-23 08:07:24

标签: git version-control merge branch rebase

我无法让git在中等复杂的分支场景中做我想做的事情。每当我使用rebase命令时,似乎我的一些工作都会丢失。我是git的新手,来自Perforce背景。我使用错误的命令还是遗漏了一些标志?我对本地存储库的内部状态的直觉是否正确?

注意:branchB依赖于branchA,而branchC依赖于branchB。

  1. git checkout master
  2. git pull master
  3. git checkout -b branchA

    ==O master
       \==A branchA
    
  4. 编辑branchA
  5. git add ...
  6. git commit
  7. git review
  8. git checkout -b branchB

    ==O==O master
       \==A branchA
           \==B branchB
    
  9. 编辑branchB
  10. 代码审核需要更改branchA
  11. git add ...
  12. git commit
  13. git checkout branchA
  14. 编辑branchA
  15. git add ...
  16. git commit --amend< ==我应该在这里使用--amend吗?
  17. git review
  18. git checkout branchB
  19. 再编辑branchB
  20. git add ...
  21. git commit
  22. git review
  23. git checkout -b branchC

    ==O==O==O==O master
      \==A branchA
          \==B branchB
              \==C branchC
    
  24. 编辑branchC
  25. branchA通过代码审核
  26. git add ...
  27. git commit
  28. git checkout master
  29. git pull master
  30. git checkout branchA
  31. git rebase master branchA
  32. 解决合并冲突
  33. git checkout master
  34. git merge branchA

    ==O==O==O==O==A master
       \==B branchB <== is this correct?
           \==C branchC
    
  35. git checkout branchB
  36. git rebase master branchB
  37. 解决合并冲突

    ==O==O==O==O==A master
          \        \==B branchB
           \==C branchC <== is this correct?
    
  38. git checkout branchC
  39. git rebase branchB branchC
  40. 解决合并冲突&lt; ==经常重做与branchB相同的合并冲突

    ==O==O==O==O==A master
                   \==B branchB
                       \==C branchC
    
  41. 设计变更需要重构branchA
  42. git checkout master
  43. git pull
  44. git checkout -b refactorA

    ==O==O==O==O==A master
                  |==A' refactorA
                  |==B branchB
                      \==C branchC
    
  45. 编辑refactorA
  46. git add ...
  47. git commit
  48. git review
  49. git checkout branchB
  50. git rebase refactorA branchB&lt; ==这是一个错误吗?
  51. 解决合并冲突(三组冲突)

    ==O==O==O==O==A master
                   \==A' refactorA
                    \  \==B branchB <== is this correct?
                     \==C branchC
    
  52. git review&lt; ==抱怨多次提交
  53. git log&lt; ==显示branchB现在有一些branchA的提交实例

1 个答案:

答案 0 :(得分:0)

您需要了解提交是前一个提交的依赖项,而分支只是指向提交图的一次提交的指针。

这是提交图的演变,数字是提交,上面是更新,引用和分支是提交,HEAD是你当前的位置。

您可以通过在每一步启动git log --graph --oneline --all --decorate来查看提交图的演变。

  1. git checkout master

    1 origin/master, HEAD -> master
    |
    0
    
  2. git pull master

    1 origin/master, HEAD -> master
    |
    0
    
  3. git checkout -b branchA

    1 origin/master, master, HEAD -> branchA
    |
    0
    
  4. 编辑branchA

  5. git add ...
  6. git commit
  7. git review

    2 HEAD -> branchA
    |
    1 origin/master, master
    |
    0
    
  8. git checkout -b branchB

    2 branchA, HEAD -> branchB
    |
    1 origin/master, master
    |
    0
    
  9. 编辑branchB

  10. 代码审核需要更改branchA
  11. git add ...
  12. git commit

    3 HEAD -> branchB
    |
    2 branchA
    |
    1 origin/master, master
    |
    0
    
  13. git checkout branchA

    3 branchB
    |
    2 HEAD -> branchA
    |
    1 origin/master, master
    |
    0
    
  14. 编辑branchA

  15. git add ...
  16. git commit --amend =&gt;通过使用--amend,您可以使用相同的父
  17. 创建新的提交
  18. git review

    3 branchB
    |
    | 2' HEAD -> branchA
    | |
    2 | 
    |/
    1 origin/master, master
    |
    0
    
  19. git checkout branchB

    3 HEAD -> branchB
    |
    | 2' branchA
    | |
    2 | 
    |/
    1 origin/master, master
    |
    0
    
  20. 再编辑branchB

  21. git add ...
  22. git commit
  23. git review

    4 HEAD -> branchB
    |
    3
    |
    | 2' branchA
    | |
    2 | 
    |/
    1 origin/master, master
    |
    0
    
  24. git checkout -b branchC

    4 branchB, HEAD -> branchC
    |
    3
    |
    | 2' branchA
    | |
    2 | 
    |/
    1 origin/master, master
    |
    0
    
  25. 编辑branchC

  26. branchA通过代码审核
  27. git add ...
  28. git commit

    5 HEAD -> branchC
    |
    4 branchB
    |
    3
    |
    | 2' branchA
    | |
    2 | 
    |/
    1 origin/master, master
    |
    0
    
  29. git checkout master

    5 branchC
    |
    4 branchB
    |
    3
    |
    | 2' branchA
    | |
    2 | 
    |/
    1 origin/master, HEAD -> master
    |
    0
    
  30. git pull master

    5 branchC
    |
    4 branchB
    |
    3
    |
    | 2' branchA
    | |
    2 |
    | |
    | | 6 origin/master, HEAD -> master
    | | |
    |/ /
    | /
    |/
    2
    |
    0
    
  31. git checkout branchA

    5 branchC
    |
    4 branchB
    |
    3
    |
    | 2' HEAD -> branchA
    | |
    2 |
    | |
    | | 6 origin/master, master
    | | |
    |/ /
    | /
    |/
    1
    |
    0
    
  32. git rebase master branchA

  33. 解决合并冲突

    5 branchC
    |
    4 branchB
    |
    3
    |
    |   2'' HEAD -> branchA
    |   |
    2   |
    |   |
    |   6 origin/master, master
    |   |
    |  /
    | /
    |/
    1
    |
    0
    
  34. git checkout master

    5 branchC
    |
    4 branchB
    |
    3
    |
    |   2'' branchA
    |   |
    2   |
    |   |
    |   6 origin/master, HEAD -> master
    |   |
    |  /
    | /
    |/
    1
    |
    0
    
  35. git merge branchA#merge is fast-forward

    5 branchC
    |
    4 branchB
    |
    3
    |
    |   2'' branchA, HEAD -> master
    |   |
    2   |
    |   |
    |   6 origin/master
    |   |
    |  /
    | /
    |/
    1
    |
    0
    
  36. git checkout branchB

    5 branchC
    |
    4 HEAD -> branchB
    |
    3
    |
    |   2'' branchA, master
    |   |
    2   |
    |   |
    |   6 origin/master
    |   |
    |  /
    | /
    |/
    1
    |
    0
    
  37. git rebase master branchB#2应该被删除,因为2''已经是分支

  38. 解决合并冲突

    5 branchC
    |
    |   4' HEAD -> branchB
    |   |
    4   |
    |   |
    |   3'
    |   |
    3   |
    |   |
    |   2'' branchA, master
    |   |
    2   |
    |   |
    |   6 origin/master
    |   |
    |  /
    | /
    |/
    1
    |
    0
    
  39. git checkout branchC

    5 HEAD -> branchC
    |
    |   4' branchB
    |   |
    4   |
    |   |
    |   3'
    |   |
    3   |
    |   |
    |   2'' branchA, master
    |   |
    2   |
    |   |
    |   6 origin/master
    |   |
    |  /
    | /
    |/
    1
    |
    0
    
  40. git rebase branchB branchC

  41. 解决合并冲突#当rebase重放branchB历史记录中不在branchC历史记录中的所有提交时,它将重放2,3,4和5;这就是为什么,如果你有冲突,你可能会再次遇到它们

        5' HEAD -> branchC
        |
        4' branchB
        |
        3'
        |
        2'' branchA, master
        |
        6 origin/master
        |
       /
      /
     /
    1
    |
    0
    
  42. 设计变更需要重构branchA

  43. git checkout master
  44. git pull
  45. git checkout -b refactorA

    5' branchC
    |
    4' branchB
    |
    3'
    |
    2'' branchA, master, HEAD -> refactorA
    |
    6 origin/master
    |
    1
    |
    0
    
  46. 编辑refactorA

  47. git add ...
  48. git commit
  49. git review

    5' branchC
    |
    4' branchB
    |
    |  7 HEAD -> refactorA
    |  |
    3' |
    | /
    2'' branchA, master
    |
    6 origin/master
    |
    1
    |
    0
    
  50. git checkout branchB

    5' branchC
    |
    4' HEAD -> branchB
    |
    |  7 refactorA
    |  |
    3' |
    | /
    2'' branchA, master
    |
    6 origin/master
    |
    1
    |
    0
    
  51. git rebase refactorA branchB&lt; ==这是一个错误吗?

  52. 解决合并冲突(三组冲突)

    5' branchC
    |
    |  4'' HEAD -> branchB
    |  |
    4' |
    |  |
    |  7 refactorA
    |  |
    3' |
    | /
    2'' branchA, master
    |
    6 origin/master
    |
    1
    |
    0
    
  53. git review

  54. git log#当您在从合并branchA的master创建的refactorA上重新设置分支branchB时,branchA提交在branchB中