简体   繁体   English

从功能分支分支并在主壁球合并后协调提交

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

I have the following situation: I've worked on a feature branch (called work1 ) to which I have an outstanding PR.我有以下情况:我在一个特性分支(称为work1 )上工作,我有一个出色的 PR。 While I wait for the PR to be approved, I want to start working on a new feature branch (called work2 ).在等待 PR 获得批准的同时,我想开始开发一个新的功能分支(称为work2 )。 The problem is that my company's merge strategy is to squash-merge into main, and I don't know how to use git to handle the new squashed merge when I try to merge work2 into main .问题是我公司的合并策略是 squash-merge 到 main 中,当我尝试将work2合并到main时,我不知道如何使用 git 来处理新的 squashed 合并。

This is pretty much what I have:这几乎就是我所拥有的:

  1. Create the repo and a file:创建 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. Create the first feature branch with a couple of commits:使用几个提交创建第一个功能分支:
$ 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. Create the second feature branch work2 which depends on code from work1 :创建依赖于来自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(+)

Now, say that the PR for work1 has been approved and it has been squashed-merged into master现在,假设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

Great, now my commits from work1 are all in master.太好了,现在我从work1提交的所有内容都在 master 中。 Let's see if I can merge work2 :让我们看看我是否可以合并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.

Of course I can't, because I squash-merged work1 .当然我不能,因为我合并work1

I've tried rebasing work2 from master , but that doesn't work either:我已经尝试从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'

The problem is again the squashed merge.问题再次是压扁的合并。 I've tried a convoluted solution using git cherry-pick based on this answer :我根据这个答案使用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'

OK, so now I have the two squashed merges in a breach.好的,所以现在我有两个压扁的合并在一个突破口。

$ 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

Which is ugly as hell because of those "squash" commits.由于那些“壁球”提交,这很丑陋。 Any suggestions on how to reconcile these squashed merges?关于如何协调这些压扁的合并有什么建议吗? Thanks a bunch.谢谢一堆。

What you can do after git merge --squash work1 is:git merge --squash work1之后你可以做的是:

  • Do not remove branch yet (to make things faster)不要删除分支(以使事情更快)
  • git checkout work2 (as @TTT rightly noticed, this step is not needed) git checkout work2 (正如@TTT 正确注意到的,这一步是不需要的)
  • git rebase --onto master work1 work2

This way you will not have to resolve any conflict and managing work2 branch will be easier.这样您就不必解决任何冲突,并且管理work2分支会更容易。 If you did remove your branch, then you need to get the last commit hash from work1 branch (for example you will find it in the commit message of the squashed commits).如果您确实删除了您的分支,那么您需要从work1分支获取最后一个提交哈希(例如,您会在压缩提交的提交消息中找到它)。

One note, the merge conflict doesn't suggest that you cannot merge.请注意,合并冲突并不意味着您不能合并。 You will be fine with merging after resolving conflicts.解决冲突后合并就可以了。 But the history wouldn't be looking great.但历史看起来不会很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM