Git - submodules HEAD is always detached after running update? -
i have configuration submodule in .gitmodules file:
[submodule "sub"] shallow = true branch = master path = sub url = https://path/to/repo.git
now want when clones repo , runs these commands:
git submodule init git submodule update
is shallow master branch of submodule. happens not checkout master
branch. detached head, need manually run git checkout master
. instead of 2 commands, user needs run 1 additional.
i looked this: why git submodule head detached master?
but advice on accepted answers, not seem help: added branch want in .gitmodules
file, added remote upstream master (this works cloned/updated repository after had checkout master myself).
so intended detached head if clones repository , wants set submodule?
i'm still investigating this, here script came , using now:
#! /bin/bash # written carlo wood 2016 echo "in \"$(pwd)\", entering $0 $*" # script should run root of parent project. if ! test -e .git; echo "$0: $(pwd) not git repository." exit 1 fi # parse command line parameters. opt_init= opt_recursive= do_foreach=0 initial_call=1 while [[ $# -gt 0 ]] case $1 in --init) opt_init=$1 ;; --recursive) opt_recursive=$1 do_foreach=1 ;; --reentry) initial_call=0 ;; --) break; ;; -*) echo "unknown option $1" exit 1 ;; *) break ;; esac shift done # determine full path script. if [[ ${0:0:1} = / ]]; full_path="$0" else full_path="$(realpath $0)" fi if test "$initial_call" -eq 1; do_foreach=1 else # script called git submodule foreach ...' name="$1" path="$2" sha1="$3" toplevel="$4" # make sure in right directory. cd "$toplevel/$path" || exit 1 # parent project want checkout branch module? submodule_branch=$(git config -f "$toplevel/.gitmodules" submodule.$name.branch) if test -n "$submodule_branch"; echo "calling 'git checkout $submodule_branch' in $(pwd)" git checkout $submodule_branch || exit 1 echo "calling 'git pull' in $(pwd)" git pull || exit 1 if test $(git rev-parse head) != "$sha1"; # update parent project point head of branch. pushd "$toplevel" >/dev/null sn1=$(git stash list | grep '^stash' | wc --lines) git stash save --quiet automatic stash of parent project update_submodules.sh sn2=$(git stash list | grep '^stash' | wc --lines) git add $name git commit -m "update of submodule $name current $submodule_branch" if test $sn1 -ne $sn2; git stash pop --quiet fi popd >/dev/null fi elif test $(git rev-parse head) != "$sha1"; # no submodule.$name.branch submodule. checkout detached head. git checkout $sha1 fi fi echo "do_foreach=$do_foreach; opt_init=$opt_init; opt_recursive=$opt_recursive; name=$name; path=$path; sha1=$sha1; toplevel=$toplevel; pwd=$(pwd)" if test $do_foreach -eq 1; if test -n "$opt_init"; echo "calling 'git submodule init'" git submodule init fi # make sure submodules exist. echo "calling 'git submodule update'" git submodule update # call script recursively submodules. echo 'calling '"'"'git submodule foreach '"$full_path --reentery $opt_init $opt_recursive"' $name $path $sha1 $toplevel'"'" git submodule foreach "$full_path --reentry $opt_init $opt_recursive"' $name $path $sha1 $toplevel' fi
calling script same 'git submodule update' , supports --init , --recursive. however, can configure submodules checkout , pull branch instead setting 'branch' value; example: git config -f .gitmodules submodule.name.branch master
cause submodule name check out , pull branch master instead of whatever current sha1 is. update parent project point head of branch.
Comments
Post a Comment