I have the following situation: I've worked on a feature branch (called work1
) to which I have an outstanding PR. While I wait for the PR to be approved, I want to start working on a new feature branch (called 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
.
This is pretty much what I have:
- Create the repo and a file:
$ 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
- 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
- Create the second feature branch
work2
which depends on code fromwork1
:
$ 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
$ 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. Let's see if I can merge 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
.
I've tried rebasing work2
from master
, but that doesn't work either:
$ 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 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.