Skip to content

Commit b5cabb4

Browse files
sunshinecogitster
authored andcommitted
rebase: refuse to switch to branch already checked out elsewhere
The invocation "git rebase <upstream> <branch>" switches to <branch> before performing the rebase operation. However, unlike git-switch, git-checkout, and git-worktree which all refuse to switch to a branch that is already checked out in some other worktree, git-rebase switches to <branch> unconditionally. Curb this careless behavior by making git-rebase also refuse to switch to a branch checked out elsewhere. Reported-by: Mike Hommey <[email protected]> Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent df126ca commit b5cabb4

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

builtin/rebase.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,10 +1953,11 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
19531953
/* Is it a local branch? */
19541954
strbuf_reset(&buf);
19551955
strbuf_addf(&buf, "refs/heads/%s", branch_name);
1956-
if (!read_ref(buf.buf, &options.orig_head))
1956+
if (!read_ref(buf.buf, &options.orig_head)) {
1957+
die_if_checked_out(buf.buf, 1);
19571958
options.head_name = xstrdup(buf.buf);
19581959
/* If not is it a valid ref (branch or commit)? */
1959-
else if (!get_oid(branch_name, &options.orig_head))
1960+
} else if (!get_oid(branch_name, &options.orig_head))
19601961
options.head_name = NULL;
19611962
else
19621963
die(_("fatal: no such branch/commit '%s'"),

t/t3400-rebase.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,4 +377,22 @@ test_expect_success 'rebase -c rebase.useBuiltin=false warning' '
377377
test_must_be_empty err
378378
'
379379

380+
test_expect_success 'switch to branch checked out here' '
381+
git checkout master &&
382+
git rebase master master
383+
'
384+
385+
test_expect_success 'switch to branch not checked out' '
386+
git checkout master &&
387+
git branch other &&
388+
git rebase master other
389+
'
390+
391+
test_expect_success 'refuse to switch to branch checked out elsewhere' '
392+
git checkout master &&
393+
git worktree add wt &&
394+
test_must_fail git -C wt rebase master master 2>err &&
395+
test_i18ngrep "already checked out" err
396+
'
397+
380398
test_done

0 commit comments

Comments
 (0)