The previous code only checked out branches in cmd_add.  This commit
moves the branch-checkout logic into module_clone, where it can be
shared by cmd_add and cmd_update.  I also update the initial checkout
command to use 'reset' to preserve branches setup during module_clone.

With this change, folks cloning submodules for the first time via:

  $ git submodule update ...

will get a local branch instead of a detached HEAD, unless they are
using the default checkout-mode updates.  This is a change from the
previous situation where cmd_update always used checkout-mode logic
(regardless of the requested update mode) for updates that triggered
an initial clone, which always resulted in a detached HEAD.

This commit does not change the logic for updates after the initial
clone, which will continue to create detached HEADs for checkout-mode
updates, and integrate remote work with the local HEAD (detached or
not) in other modes.

The motivation for the change is that developers doing local work
inside the submodule are likely to select a non-checkout-mode for
updates so their local work is integrated with upstream work.
Developers who are not doing local submodule work stick with
checkout-mode updates so any apparently local work is blown away
during updates.  For example, if upstream rolls back the remote branch
or gitlinked commit to an earlier version, the checkout-mode developer
wants their old submodule checkout to be rolled back as well, instead
of getting a no-op merge/rebase with the rolled-back reference.

By using the update mode to distinguish submodule developers from
black-box submodule consumers, we can setup local branches for the
developers who will want local branches, and stick with detached HEADs
for the developers that don't care.

Signed-off-by: W. Trevor King <>
--- | 53 ++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 36 insertions(+), 17 deletions(-)

diff --git a/ b/
index 68dcbe1..4a09951 100755
--- a/
+++ b/
@@ -246,6 +246,9 @@ module_name()
 # $3 = URL to clone
 # $4 = reference repository to reuse (empty for independent)
 # $5 = depth argument for shallow clones (empty for deep)
+# $6 = (remote-tracking) starting point for the local branch (empty for HEAD)
+# $7 = local branch to create (empty for a detached HEAD, unless $6 is
+#      also empty, in which case the local branch is left unchanged)
 # Prior to calling, cmd_update checks that a possibly existing
 # path is not a git repository.
@@ -259,6 +262,8 @@ module_clone()
+       start_point="$6"
+       local_branch="$7"
        if test -n "$GIT_QUIET"
@@ -312,7 +317,16 @@ module_clone()
        echo "gitdir: $rel/$a" >"$sm_path/.git"
        rel=$(echo $a | sed -e 's|[^/][^/]*|..|g')
-       (clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config 
core.worktree "$rel/$b")
+       (
+               clear_local_git_env
+               cd "$sm_path" &&
+               GIT_WORK_TREE=. git config core.worktree "$rel/$b" &&
+               # ash fails to wordsplit ${local_branch:+-B "$local_branch"...}
+               case "$local_branch" in
+               '') git checkout -f -q ${start_point:+"$start_point"} ;;
+               ?*) git checkout -f -q -B "$local_branch" 
${start_point:+"$start_point"} ;;
+               esac
+       ) || die "$(eval_gettext "Unable to setup cloned submodule 
@@ -475,16 +489,14 @@ Use -f if you really want to add it." >&2
                                echo "$(eval_gettext "Reactivating local git 
directory for submodule '\$sm_name'.")"
-               module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" 
"$depth" || exit
-               (
-                       clear_local_git_env
-                       cd "$sm_path" &&
-                       # ash fails to wordsplit ${branch:+-b "$branch"...}
-                       case "$branch" in
-                       '') git checkout -f -q ;;
-                       ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
-                       esac
-               ) || die "$(eval_gettext "Unable to checkout submodule 
+               if test -n "$branch"
+               then
+                       start_point="origin/$branch"
+                       local_branch="$branch"
+               else
+                       start_point=""
+               fi
+               module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" 
"$depth" "$start_point" "$local_branch" || exit
        git config submodule."$sm_name".url "$realrepo"
@@ -803,7 +815,9 @@ cmd_update()
                name=$(module_name "$sm_path") || exit
                url=$(git config submodule."$name".url)
-               branch=$(get_submodule_config "$name" branch master)
+               config_branch=$(get_submodule_config "$name" branch)
+               branch="${config_branch:-master}"
+               local_branch="$branch"
                if ! test -z "$update"
@@ -817,11 +831,15 @@ cmd_update()
                displaypath=$(relative_path "$prefix$sm_path")
-               if test "$update_module" = "none"
-               then
+               case "$update_module" in
+               none)
                        echo "Skipping submodule '$displaypath'"
-               fi
+                       ;;
+               checkout)
+                       local_branch=""
+                       ;;
+               esac
                if test -z "$url"
@@ -835,7 +853,8 @@ Maybe you want to use 'update --init'?")"
                if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
-                       module_clone "$sm_path" "$name" "$url" "$reference" 
"$depth" || exit
+                       start_point="origin/${branch}"
+                       module_clone "$sm_path" "$name" "$url" "$reference" 
"$depth" "$start_point" "$local_branch" || exit
@@ -881,7 +900,7 @@ Maybe you want to use 'update --init'?")"
                        case ";$cloned_modules;" in
                                # then there is no local change to integrate
-                               update_module=checkout ;;
+                               update_module='!git reset --hard -q'

To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to
More majordomo info at

Reply via email to