我无法让git在中等复杂的分支场景中做我想做的事情。每当我使用rebase命令时,似乎我的一些工作都会丢失。我是git的新手,来自Perforce背景。我使用错误的命令还是遗漏了一些标志?我对本地存储库的内部状态的直觉是否正确?
注意:branchB依赖于branchA,而branchC依赖于branchB。
git checkout -b branchA
==O master
\==A branchA
git checkout -b branchB
==O==O master
\==A branchA
\==B branchB
git checkout -b branchC
==O==O==O==O master
\==A branchA
\==B branchB
\==C branchC
git merge branchA
==O==O==O==O==A master
\==B branchB <== is this correct?
\==C branchC
解决合并冲突
==O==O==O==O==A master
\ \==B branchB
\==C branchC <== is this correct?
解决合并冲突&lt; ==经常重做与branchB相同的合并冲突
==O==O==O==O==A master
\==B branchB
\==C branchC
git checkout -b refactorA
==O==O==O==O==A master
|==A' refactorA
|==B branchB
\==C branchC
解决合并冲突(三组冲突)
==O==O==O==O==A master
\==A' refactorA
\ \==B branchB <== is this correct?
\==C branchC
答案 0 :(得分:0)
您需要了解提交是前一个提交的依赖项,而分支只是指向提交图的一次提交的指针。
这是提交图的演变,数字是提交,上面是更新,引用和分支是提交,HEAD
是你当前的位置。
您可以通过在每一步启动git log --graph --oneline --all --decorate
来查看提交图的演变。
git checkout master
1 origin/master, HEAD -> master
|
0
git pull master
1 origin/master, HEAD -> master
|
0
git checkout -b branchA
1 origin/master, master, HEAD -> branchA
|
0
编辑branchA
git review
2 HEAD -> branchA
|
1 origin/master, master
|
0
git checkout -b branchB
2 branchA, HEAD -> branchB
|
1 origin/master, master
|
0
编辑branchB
git commit
3 HEAD -> branchB
|
2 branchA
|
1 origin/master, master
|
0
git checkout branchA
3 branchB
|
2 HEAD -> branchA
|
1 origin/master, master
|
0
编辑branchA
--amend
,您可以使用相同的父git review
3 branchB
|
| 2' HEAD -> branchA
| |
2 |
|/
1 origin/master, master
|
0
git checkout branchB
3 HEAD -> branchB
|
| 2' branchA
| |
2 |
|/
1 origin/master, master
|
0
再编辑branchB
git review
4 HEAD -> branchB
|
3
|
| 2' branchA
| |
2 |
|/
1 origin/master, master
|
0
git checkout -b branchC
4 branchB, HEAD -> branchC
|
3
|
| 2' branchA
| |
2 |
|/
1 origin/master, master
|
0
编辑branchC
git commit
5 HEAD -> branchC
|
4 branchB
|
3
|
| 2' branchA
| |
2 |
|/
1 origin/master, master
|
0
git checkout master
5 branchC
|
4 branchB
|
3
|
| 2' branchA
| |
2 |
|/
1 origin/master, HEAD -> master
|
0
git pull master
5 branchC
|
4 branchB
|
3
|
| 2' branchA
| |
2 |
| |
| | 6 origin/master, HEAD -> master
| | |
|/ /
| /
|/
2
|
0
git checkout branchA
5 branchC
|
4 branchB
|
3
|
| 2' HEAD -> branchA
| |
2 |
| |
| | 6 origin/master, master
| | |
|/ /
| /
|/
1
|
0
git rebase master branchA
解决合并冲突
5 branchC
|
4 branchB
|
3
|
| 2'' HEAD -> branchA
| |
2 |
| |
| 6 origin/master, master
| |
| /
| /
|/
1
|
0
git checkout master
5 branchC
|
4 branchB
|
3
|
| 2'' branchA
| |
2 |
| |
| 6 origin/master, HEAD -> master
| |
| /
| /
|/
1
|
0
git merge branchA#merge is fast-forward
5 branchC
|
4 branchB
|
3
|
| 2'' branchA, HEAD -> master
| |
2 |
| |
| 6 origin/master
| |
| /
| /
|/
1
|
0
git checkout branchB
5 branchC
|
4 HEAD -> branchB
|
3
|
| 2'' branchA, master
| |
2 |
| |
| 6 origin/master
| |
| /
| /
|/
1
|
0
git rebase master branchB#2应该被删除,因为2''已经是分支
解决合并冲突
5 branchC
|
| 4' HEAD -> branchB
| |
4 |
| |
| 3'
| |
3 |
| |
| 2'' branchA, master
| |
2 |
| |
| 6 origin/master
| |
| /
| /
|/
1
|
0
git checkout branchC
5 HEAD -> branchC
|
| 4' branchB
| |
4 |
| |
| 3'
| |
3 |
| |
| 2'' branchA, master
| |
2 |
| |
| 6 origin/master
| |
| /
| /
|/
1
|
0
git rebase branchB branchC
解决合并冲突#当rebase重放branchB历史记录中不在branchC历史记录中的所有提交时,它将重放2,3,4和5;这就是为什么,如果你有冲突,你可能会再次遇到它们
5' HEAD -> branchC
|
4' branchB
|
3'
|
2'' branchA, master
|
6 origin/master
|
/
/
/
1
|
0
设计变更需要重构branchA
git checkout -b refactorA
5' branchC
|
4' branchB
|
3'
|
2'' branchA, master, HEAD -> refactorA
|
6 origin/master
|
1
|
0
编辑refactorA
git review
5' branchC
|
4' branchB
|
| 7 HEAD -> refactorA
| |
3' |
| /
2'' branchA, master
|
6 origin/master
|
1
|
0
git checkout branchB
5' branchC
|
4' HEAD -> branchB
|
| 7 refactorA
| |
3' |
| /
2'' branchA, master
|
6 origin/master
|
1
|
0
git rebase refactorA branchB&lt; ==这是一个错误吗?
解决合并冲突(三组冲突)
5' branchC
|
| 4'' HEAD -> branchB
| |
4' |
| |
| 7 refactorA
| |
3' |
| /
2'' branchA, master
|
6 origin/master
|
1
|
0
git review