Skip to content

Commit f818536

Browse files
committed
Merge branch 'jc/rebase-detach-fix'
"git rebase $base $non_branch_commit", when $base is an ancestor or the $non_branch_commit, modified the current branch, which has been corrected. * jc/rebase-detach-fix: rebase: set REF_HEAD_DETACH in checkout_up_to_date() rebase: use test_commit helper in setup
2 parents 5fe35fc + bdff97a commit f818536

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

builtin/rebase.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,8 @@ static int checkout_up_to_date(struct rebase_options *options)
829829
ropts.oid = &options->orig_head;
830830
ropts.branch = options->head_name;
831831
ropts.flags = RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
832+
if (!ropts.branch)
833+
ropts.flags |= RESET_HEAD_DETACH;
832834
ropts.head_msg = buf.buf;
833835
if (reset_head(the_repository, &ropts) < 0)
834836
ret = error(_("could not switch to %s"), options->switch_to);

t/t3400-rebase.sh

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ GIT_AUTHOR_EMAIL=bogus@email@address
1818
export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
1919

2020
test_expect_success 'prepare repository with topic branches' '
21-
git config core.logAllRefUpdates true &&
22-
echo First >A &&
23-
git update-index --add A &&
24-
git commit -m "Add A." &&
21+
test_commit "Add A." A First First &&
2522
git checkout -b force-3way &&
2623
echo Dummy >Y &&
2724
git update-index --add Y &&
@@ -32,9 +29,7 @@ test_expect_success 'prepare repository with topic branches' '
3229
git mv A D/A &&
3330
git commit -m "Move A." &&
3431
git checkout -b my-topic-branch main &&
35-
echo Second >B &&
36-
git update-index --add B &&
37-
git commit -m "Add B." &&
32+
test_commit "Add B." B Second Second &&
3833
git checkout -f main &&
3934
echo Third >>A &&
4035
git update-index A &&
@@ -399,6 +394,15 @@ test_expect_success 'switch to branch not checked out' '
399394
git rebase main other
400395
'
401396

397+
test_expect_success 'switch to non-branch detaches HEAD' '
398+
git checkout main &&
399+
old_main=$(git rev-parse HEAD) &&
400+
git rebase First Second^0 &&
401+
test_cmp_rev HEAD Second &&
402+
test_cmp_rev main $old_main &&
403+
test_must_fail git symbolic-ref HEAD
404+
'
405+
402406
test_expect_success 'refuse to switch to branch checked out elsewhere' '
403407
git checkout main &&
404408
git worktree add wt &&

0 commit comments

Comments
 (0)