Skip to content

Commit d2172ef

Browse files
bwijengitster
authored andcommitted
builtin/rebase.c: make sure the active branch isn't moved when autostashing
Consider the following scenario: git checkout not-the-master work work work git rebase --autostash upstream master Here 'rebase --autostash <upstream> <branch>' incorrectly moves the active branch (not-the-master) to master (before the rebase). The expected behavior: (5879477:/git-rebase.sh:526) AUTOSTASH=$(git stash create autostash) git reset --hard git checkout master git rebase upstream git stash apply $AUTOSTASH The actual behavior: (6defce2:/builtin/rebase.c:1062) AUTOSTASH=$(git stash create autostash) git reset --hard master git checkout master git rebase upstream git stash apply $AUTOSTASH This commit reinstates the 'legacy script' behavior as introduced with 5879477: rebase: implement --[no-]autostash and rebase.autostash Signed-off-by: Ben Wijen <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5fa0f52 commit d2172ef

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

builtin/rebase.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1968,9 +1968,12 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
19681968
state_dir_path("autostash", &options);
19691969
struct child_process stash = CHILD_PROCESS_INIT;
19701970
struct object_id oid;
1971-
struct commit *head =
1972-
lookup_commit_reference(the_repository,
1973-
&options.orig_head);
1971+
struct object_id head_oid;
1972+
struct commit *head;
1973+
1974+
if (get_oid("HEAD", &head_oid))
1975+
die(_("could not determine HEAD revision"));
1976+
head = lookup_commit_reference(the_repository, &head_oid);
19741977

19751978
argv_array_pushl(&stash.args,
19761979
"stash", "create", "autostash", NULL);

t/t3420-rebase-autostash.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,4 +306,12 @@ test_expect_success 'branch is left alone when possible' '
306306
test unchanged-branch = "$(git rev-parse --abbrev-ref HEAD)"
307307
'
308308

309+
test_expect_success 'never change active branch' '
310+
git checkout -b not-the-feature-branch unrelated-onto-branch &&
311+
test_when_finished "git reset --hard && git checkout master" &&
312+
echo changed >file0 &&
313+
git rebase --autostash not-the-feature-branch feature-branch &&
314+
test_cmp_rev not-the-feature-branch unrelated-onto-branch
315+
'
316+
309317
test_done

0 commit comments

Comments
 (0)