Skip to content

Commit c192f9c

Browse files
andrewkwwgitster
authored andcommitted
git-rebase--interactive.sh: preserve-merges fails on merges created with no-ff
'git rebase' uses 'git merge' to preserve merges (-p). This preserves the original merge commit correctly, except when the original merge commit was created by 'git merge --no-ff'. In this case, 'git rebase' will fail to preserve the merge, because during 'git rebase', 'git merge' will simply fast-forward and skip the commit. For example: B / \ A---M / ---o---O---P---Q If we try to rebase M onto P, we lose the merge commit and this happens: A---B / ---o---O---P---Q To correct this, we simply do a "no fast-forward" on all merge commits when rebasing. Since by the time we decided to do a 'git merge' inside 'git rebase', it means there was a merge originally, so 'git merge' should always create a merge commit regardless of what the merge branches look like. This way, when rebase M onto P from the above example, we get: B / \ A---M / ---o---O---P---Q Signed-off-by: Andrew Wong <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4fec830 commit c192f9c

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

git-rebase--interactive.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ pick_one_preserving_merges () {
339339
# No point in merging the first parent, that's HEAD
340340
new_parents=${new_parents# $first_parent}
341341
if ! do_with_author output \
342-
git merge $STRATEGY -m "$msg" $new_parents
342+
git merge --no-ff $STRATEGY -m "$msg" $new_parents
343343
then
344344
printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
345345
die_with_patch $sha1 "Error redoing merge $sha1"

t/t3409-rebase-preserve-merges.sh

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,17 @@ export GIT_AUTHOR_EMAIL
2727
# \
2828
# B2 <-- origin/topic
2929
#
30-
# In both cases, 'topic' is rebased onto 'origin/topic'.
30+
# Clone 3 (no-ff merge):
31+
#
32+
# A1--A2--B3 <-- origin/master
33+
# \
34+
# B1------M <-- topic
35+
# \ /
36+
# \--A3 <-- topic2
37+
# \
38+
# B2 <-- origin/topic
39+
#
40+
# In all cases, 'topic' is rebased onto 'origin/topic'.
3141

3242
test_expect_success 'setup for merge-preserving rebase' \
3343
'echo First > A &&
@@ -61,6 +71,16 @@ test_expect_success 'setup for merge-preserving rebase' \
6171
git commit -m "Merge origin/master into topic"
6272
) &&
6373
74+
git clone ./. clone3 &&
75+
(
76+
cd clone3 &&
77+
git checkout -b topic2 origin/topic &&
78+
echo Sixth > A &&
79+
git commit -a -m "Modify A3" &&
80+
git checkout -b topic origin/topic &&
81+
git merge --no-ff topic2
82+
) &&
83+
6484
git checkout topic &&
6585
echo Fourth >> B &&
6686
git commit -a -m "Modify B2"
@@ -93,4 +113,14 @@ test_expect_success '--continue works after a conflict' '
93113
)
94114
'
95115

116+
test_expect_success 'rebase -p preserves no-ff merges' '
117+
(
118+
cd clone3 &&
119+
git fetch &&
120+
git rebase -p origin/topic &&
121+
test 3 = $(git rev-list --all --pretty=oneline | grep "Modify A" | wc -l) &&
122+
test 1 = $(git rev-list --all --pretty=oneline | grep "Merge branch" | wc -l)
123+
)
124+
'
125+
96126
test_done

0 commit comments

Comments
 (0)