Skip to content

Commit 0b75572

Browse files
committed
Merge branch 'sb/submodule-recursive-checkout-detach-head'
"git checkout --recursive" may overwrite and rewind the history of the branch that happens to be checked out in submodule repositories, which might not be desirable. Detach the HEAD but still allow the recursive checkout to succeed in such a case. * sb/submodule-recursive-checkout-detach-head: Documentation/checkout: clarify submodule HEADs to be detached recursive submodules: detach HEAD from new state
2 parents 1a4e40a + 57f22bf commit 0b75572

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

Documentation/git-checkout.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ section of linkgit:git-add[1] to learn how to operate the `--patch` mode.
264264
local modifications in a submodule would be overwritten the checkout
265265
will fail unless `-f` is used. If nothing (or --no-recurse-submodules)
266266
is used, the work trees of submodules will not be updated.
267+
Just like linkgit:git-submodule[1], this will detach the
268+
submodules HEAD.
267269

268270
<branch>::
269271
Branch to checkout; if it refers to a branch (i.e., a name that,

submodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1670,7 +1670,8 @@ int submodule_move_head(const char *path,
16701670
cp.dir = path;
16711671

16721672
prepare_submodule_repo_env(&cp.env_array);
1673-
argv_array_pushl(&cp.args, "update-ref", "HEAD", new, NULL);
1673+
argv_array_pushl(&cp.args, "update-ref", "HEAD",
1674+
"--no-deref", new, NULL);
16741675

16751676
if (run_command(&cp)) {
16761677
ret = -1;

t/lib-submodule-update.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,23 @@ test_submodule_switch_recursing_with_args () {
689689
test_submodule_content sub1 origin/add_sub1
690690
)
691691
'
692+
test_expect_success "$command: submodule branch is not changed, detach HEAD instead" '
693+
prolog &&
694+
reset_work_tree_to_interested add_sub1 &&
695+
(
696+
cd submodule_update &&
697+
git -C sub1 checkout -b keep_branch &&
698+
git -C sub1 rev-parse HEAD >expect &&
699+
git branch -t check-keep origin/modify_sub1 &&
700+
$command check-keep &&
701+
test_superproject_content origin/modify_sub1 &&
702+
test_submodule_content sub1 origin/modify_sub1 &&
703+
git -C sub1 rev-parse keep_branch >actual &&
704+
test_cmp expect actual &&
705+
test_must_fail git -C sub1 symbolic-ref HEAD
706+
)
707+
'
708+
692709
# Replacing a tracked file with a submodule produces a checked out submodule
693710
test_expect_success "$command: replace tracked file with submodule checks out submodule" '
694711
prolog &&

0 commit comments

Comments
 (0)