簡體   English   中英

從功能分支分支並在主壁球合並后協調提交

[英]Branching from a feature branch and reconciling commits after main squash merge

我有以下情況:我在一個特性分支(稱為work1 )上工作,我有一個出色的 PR。 在等待 PR 獲得批准的同時,我想開始開發一個新的功能分支(稱為work2 )。 問題是我公司的合並策略是 squash-merge 到 main 中,當我嘗試將work2合並到main時,我不知道如何使用 git 來處理新的 squashed 合並。

這幾乎就是我所擁有的:

  1. 創建 repo 和一個文件:
$ mkdir git_test && cd git_test && git init .
$ echo "test!" > test.txt && git add test.txt && git commit -m "added test"
[master (root-commit) 217c4bb] added test
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
  1. 使用幾個提交創建第一個功能分支:
$ git checkout -b work1
Switched to a new branch 'work1'
$ echo "1. Added this!" >> test.txt && git add test.txt && git commit -m "Wrote '1. Added this'"
[work1 067ee68] Wrote '1. Added this'
 1 file changed, 1 insertion(+)
$ echo "2. Added more" >> test.txt && git add test.txt && git commit -m "Wrote '2. Added more'"
[work1 458d4ea] Wrote '2. Added more'
 1 file changed, 1 insertion(+)
$ git log
commit 458d4eaf8e0311d88597c54b407e73546b09cf94 (HEAD -> work1)
Author: Homer <homer@abc.example>
Date:   Fri Jun 24 16:28:10 2022 -0400

    Wrote '2. Added more'

commit 067ee68b65000f290952e0bb12ee5dda0e54b61b
Author: Homer <homer@abc.example>
Date:   Fri Jun 24 16:27:47 2022 -0400

    Wrote '1. Added this'

commit 217c4bb80ec6435589d0f5d0cc823a0801ea58b4 (master)
Author: Homer <homer@abc.example>
Date:   Fri Jun 24 16:27:15 2022 -0400

    added test
  1. 創建依賴於來自work1 work2
$ git checkout -b work2
Switched to a new branch 'work2'
$ echo "3. Added even more" >> test.txt && git add test.txt && git commit -m "Wrote '3. Added even more' in branch work2"
[work2 7bbee44] Wrote '3. Added even more' in branch work2
 1 file changed, 1 insertion(+)
$ echo "4. last commit that will finally fix the CI" >> test.txt && git add test.txt && git commit -m "'4. last commit that will finally fix the CI' in branch work2"
[work2 5cf7047] 4. last commit that will finally fix the CI' in branch work2
 1 file changed, 1 insertion(+)

現在,假設work1的 PR 已獲得批准,並且已被壓縮合並到master

$ git checkout master 
Switched to branch 'master'
$ git merge --squash work1 
Updating 217c4bb..458d4ea
Fast-forward
Squash commit -- not updating HEAD
 test.txt | 2 ++
 1 file changed, 2 insertions(+)
$ git commit
[master d3279c0] Squashed commit of the following:
 1 file changed, 2 insertions(+)
$ git log
commit d3279c0a83d57242d23869035f423acf4582dd1b (HEAD -> master)
Author: Homer <homer@abc.example>
Date:   Fri Jun 24 16:33:19 2022 -0400

    Squashed commit of the following:
    
    commit 458d4eaf8e0311d88597c54b407e73546b09cf94
    Author: Homer <homer@abc.example>
    Date:   Fri Jun 24 16:28:10 2022 -0400
    
        Wrote '2. Added more'
    
    commit 067ee68b65000f290952e0bb12ee5dda0e54b61b
    Author: Homer <homer@abc.example>
    Date:   Fri Jun 24 16:27:47 2022 -0400
    
        Wrote '1. Added this'

commit 217c4bb80ec6435589d0f5d0cc823a0801ea58b4
Author: Homer <homer@abc.example>
Date:   Fri Jun 24 16:27:15 2022 -0400

    added test

太好了,現在我從work1提交的所有內容都在 master 中。 讓我們看看我是否可以合並work2

$ git merge --no-commit --no-ff work2 
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the result.

當然我不能,因為我合並work1

我已經嘗試從master work2 ,但這也不起作用:

$ git rebase master work2
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
error: could not apply 067ee68... Wrote '1. Added this'
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 067ee68... Wrote '1. Added this'

問題再次是壓扁的合並。 我根據這個答案使用git cherry-pick嘗試了一個復雜的解決方案:

$ git checkout work2
$ git checkout -b tmp_branch
Switched to a new branch 'tmp_branch'
$ git reset --hard HEAD~2
HEAD is now at 458d4ea Wrote '2. Added more'
$ git merge --squash HEAD@{1}
Updating 458d4ea..5cf7047
Fast-forward
Squash commit -- not updating HEAD
 test.txt | 2 ++
 1 file changed, 2 insertions(+)
$ cat test.txt 
test!
1. Added this!
2. Added more
3. Added even more
4. last commit that will finally fix the CI
$ git commit
[tmp_branch f8eac44] Squashed commit of the following:
 1 file changed, 2 insertions(+)
$ git log
commit f8eac44184cd317296d94c4307c076941926b3a8 (HEAD -> tmp_branch)
Author: Homer <homer@abc.example>
Date:   Fri Jun 24 16:49:44 2022 -0400

    Squashed commit of the following:
    
    commit 5cf7047ca2f041b35d06d81423937e16ddd3cdb9
    Author: Homer <homer@abc.example>
    Date:   Fri Jun 24 16:44:05 2022 -0400
    
        4. 'last commit that will finally fix the CI' in branch work2
    
    commit 7bbee444fb592cb77515f2ed2532ada48aaae1f3
    Author: Homer <homer@abc.example>
    Date:   Fri Jun 24 16:29:11 2022 -0400
    
        Wrote '3. Added even more' in branch work2

commit 458d4eaf8e0311d88597c54b407e73546b09cf94 (work1)
Author: Homer <homer@abc.example>
Date:   Fri Jun 24 16:28:10 2022 -0400

    Wrote '2. Added more'

commit 067ee68b65000f290952e0bb12ee5dda0e54b61b
Author: Homer <homer@abc.example>
Date:   Fri Jun 24 16:27:47 2022 -0400

    Wrote '1. Added this'

commit 217c4bb80ec6435589d0f5d0cc823a0801ea58b4
Author: Homer <homer@abc.example>
Date:   Fri Jun 24 16:27:15 2022 -0400

    added test
$ git checkout master
$ git checkout -b work2_merge
Switched to a new branch 'work2_merge'
$ git cherry-pick -x f8eac44184cd317296d94c4307c076941926b3a8
[work2_merge 105c732] Squashed commit of the following:
 Date: Fri Jun 24 16:49:44 2022 -0400
 1 file changed, 2 insertions(+)
$ git log
commit 105c73247e38015fb4225c5b8f08aeac09d676f1 (HEAD -> work2_merge)
Author: Homer <homer@abc.example>
Date:   Fri Jun 24 16:49:44 2022 -0400

    Squashed commit of the following:
    
    commit 5cf7047ca2f041b35d06d81423937e16ddd3cdb9
    Author: Homer <homer@abc.example>
    Date:   Fri Jun 24 16:44:05 2022 -0400
    
        4. 'last commit that will finally fix the CI' in branch work2
    
    commit 7bbee444fb592cb77515f2ed2532ada48aaae1f3
    Author: Homer <homer@abc.example>
    Date:   Fri Jun 24 16:29:11 2022 -0400
    
        Wrote '3. Added even more' in branch work2
    
    (cherry picked from commit f8eac44184cd317296d94c4307c076941926b3a8)

commit d3279c0a83d57242d23869035f423acf4582dd1b (master)
Author: Homer <homer@abc.example>
Date:   Fri Jun 24 16:33:19 2022 -0400

    Squashed commit of the following:
    
    commit 458d4eaf8e0311d88597c54b407e73546b09cf94
    Author: Homer <homer@abc.example>
    Date:   Fri Jun 24 16:28:10 2022 -0400
    
        Wrote '2. Added more'
    
    commit 067ee68b65000f290952e0bb12ee5dda0e54b61b
    Author: Homer <homer@abc.example>
    Date:   Fri Jun 24 16:27:47 2022 -0400
    
        Wrote '1. Added this'

好的,所以現在我有兩個壓扁的合並在一個突破口。

$ git checkout master
$ git merge --squash work2_merge
Updating d3279c0..062b2a7
Fast-forward
Squash commit -- not updating HEAD
 test.txt | 2 ++
 1 file changed, 2 insertions(+)
$ git commit
[master dc31dde] Squashed commit of the following:
 1 file changed, 2 insertions(+)
$ git log
commit dc31dde347d0ea757037b294ca718a552beaa55d
Author: Homer <homer@abc.example>
Date:   Fri Jun 24 17:18:56 2022 -0400

    Squashed commit of the following:
    
    commit 062b2a792bcd408581f359771a3cbb3c3cad5d93
    Author: Homer <homer@abc.example>
    Date:   Fri Jun 24 17:13:33 2022 -0400
    
        Squashed commit of the following:
    
        commit 8b7aa42031670640350fbf8313cf83f63bd8db89
        Author: Homer <homer@abc.example>
        Date:   Fri Jun 24 16:44:05 2022 -0400
    
            '4. last commit that will finally fix the CI' in branch work2
    
        commit 7bbee444fb592cb77515f2ed2532ada48aaae1f3
        Author: Homer <homer@abc.example>
        Date:   Fri Jun 24 16:29:11 2022 -0400
    
            Wrote '3. Added even more' in branch work2
    
        (cherry picked from commit e34f0397c99de5a59a0f09f5da43305efbd3feb6)

commit d3279c0a83d57242d23869035f423acf4582dd1b
Author: Homer <homer@abc.example>
Date:   Fri Jun 24 16:33:19 2022 -0400

    Squashed commit of the following:
    
    commit 458d4eaf8e0311d88597c54b407e73546b09cf94
    Author: Homer <homer@abc.example>
    Date:   Fri Jun 24 16:28:10 2022 -0400
    
        Wrote '2. Added more'
    
    commit 067ee68b65000f290952e0bb12ee5dda0e54b61b
    Author: Homer <homer@abc.example>
    Date:   Fri Jun 24 16:27:47 2022 -0400
    
        Wrote '1. Added this'

commit 217c4bb80ec6435589d0f5d0cc823a0801ea58b4
Author: Homer <homer@abc.example>
Date:   Fri Jun 24 16:27:15 2022 -0400

    added test

由於那些“壁球”提交,這很丑陋。 關於如何協調這些壓扁的合並有什么建議嗎? 謝謝一堆。

git merge --squash work1之后你可以做的是:

  • 不要刪除分支(以使事情更快)
  • git checkout work2 (正如@TTT 正確注意到的,這一步是不需要的)
  • git rebase --onto master work1 work2

這樣您就不必解決任何沖突,並且管理work2分支會更容易。 如果您確實刪除了您的分支,那么您需要從work1分支獲取最后一個提交哈希(例如,您會在壓縮提交的提交消息中找到它)。

請注意,合並沖突並不意味着您不能合並。 解決沖突后合並就可以了。 但歷史看起來不會很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM