从功能分支分支并在主南瓜合并后协调提交

Leo*_*rdo 7 git version-control git-merge git-squash git-merge-conflict

我有以下情况:我曾在一个功能分支(称为work1)上工作,我对该分支有出色的 PR。当我等待 PR 获得批准时,我想开始开发一个新的功能分支(称为work2)。问题是我公司的合并策略是压缩合并到 main 中,当我尝试合并work2main.

这几乎就是我所拥有的:

  1. 创建存储库和文件:
$ 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
Run Code Online (Sandbox Code Playgroud)
  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
Run Code Online (Sandbox Code Playgroud)
  1. 创建第二个功能分支work2,该分支依赖于以下代码work1
$ 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(+)
Run Code Online (Sandbox Code Playgroud)

现在,假设 PRwork1已获得批准,并且已被压缩并合并为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
Run Code Online (Sandbox Code Playgroud)

太好了,现在我的提交都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.
Run Code Online (Sandbox Code Playgroud)

我当然不能,因为我挤压合并了work1

我尝试过从 变基work2master但这也不起作用:

$ 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'
Run Code Online (Sandbox Code Playgroud)

问题又是合并被压扁。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'
Run Code Online (Sandbox Code Playgroud)

好的,现在我有两个被压扁的合并出现了缺口。

$ 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
Run Code Online (Sandbox Code Playgroud)

由于那些“挤压”提交,这真是丑陋极了。关于如何协调这些被压缩的合并有什么建议吗?非常感谢。

kad*_*ewu 4

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

  • 暂时不要删除分支(以使事情更快)
  • git checkout work2(@TTT 正确地注意到,这一步是不需要的)
  • git rebase --onto master work1 work2

这样您就不必解决任何冲突,并且管理work2分支会更容易。如果您确实删除了分支,那么您需要从分支获取最后一次提交哈希work1(例如,您将在压缩提交的提交消息中找到它)。

需要注意的是,合并冲突并不意味着您不能合并。解决冲突后就可以进行合并了。但历史看起来并不美好。

  • 您实际上可以删除 checkout 命令,因为第二个命令暗示了它。 (2认同)