2016-12-22 7 views
0

私は.gitmodulesファイルにサブモジュールのために、この構成を持っている:誰かが私のレポのクローンして、これらのコマンドを実行したときにGit-submodules更新を実行した後、HEADは常に切り離されますか?

[submodule "sub"] 
    shallow = true 
    branch = master 
    path = sub 
    url = https://path/to/repo.git 

今、私がしたい:

git submodule init 
git submodule update 

は、サブモジュールの浅いmasterブランチを取得することです。しかし、それはmasterブランチにチェックアウトしていません。それはいつも分離頭になるので、手動でgit checkout masterを実行する必要があります。したがって、これらの2つのコマンドの代わりに、1つのコマンドだけを実行する必要があります。

私はこれに見えた:Why is my GIT Submodule HEAD detached from master?

しかし、受け入れられた答えにあったすべてのアドバイスは、助けていないようです:私はマスターにリモート上流追加、.gitmodulesファイルに私が欲しいの枝を追加した(ため、この唯一の作品私が自分自身を習得するためにチェックアウトしなければならなかった後、既にクローンされた/更新されたリポジトリ)

これは、誰かが自分のリポジトリをクローンしてサブモジュールを設定したい場合に、常にHEADを分離することを意図していますか?

+0

はい、 'git submodule update'は、常にサブモジュールのための分離HEADを作成します。これは、サブモジュールがサブモジュールである間にサブモジュールで開発を行う場合(他のモジュールをチェックアウトするのではなく)、非常に面倒です。作業を行う前にまずcheckout branch_nameを実行し、コミット後に親プロジェクトに戻って変更を追加することを覚えておく必要があります。私は、もっと便利な方法でこれを自動化するための答えを探しています。 –

答えて

0

は、私はまだこれを調査し、しかし、ここで私が思い付いたスクリプトで、現在使用していますよ:

#! /bin/bash                                             
# Written by Carlo Wood 2016                                         

echo "In \"$(pwd)\", entering $0 $*"                                       

# This script should be run from the root of the parent project.                                
if ! test -e .git; then                                          
    echo "$0: $(pwd) is not a git repository."                                     
    exit 1                                              
fi                                               

# Parse command line parameters.                                        
opt_init=                                              
opt_recursive=                                            
do_foreach=0                                             
initial_call=1                                            
while [[ $# -gt 0 ]]                                           
do                                               
    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 the full path to this script. 
if [[ ${0:0:1} =/]]; then 
    FULL_PATH="$0" 
else 
    FULL_PATH="$(realpath $0)" 
fi 

if test "$initial_call" -eq 1; then 
    do_foreach=1 
else 
    # Script is called from git submodule foreach ...' 
    name="$1" 
    path="$2" 
    sha1="$3" 
    toplevel="$4" 
    # Make sure we are in the right directory. 
    cd "$toplevel/$path" || exit 1 
    # Does the parent project want us to checkout a branch for this module? 
    SUBMODULE_BRANCH=$(git config -f "$toplevel/.gitmodules" submodule.$name.branch) 
    if test -n "$SUBMODULE_BRANCH"; then 
    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"; then 
     # Update the parent project to point to the head of this branch. 
     pushd "$toplevel" >/dev/null 
     SN1=$(git stash list | grep '^stash' | wc --lines) 
     git stash save --quiet Automatic stash of parent project by update_submodules.sh 
     SN2=$(git stash list | grep '^stash' | wc --lines) 
     git add $name 
     git commit -m "Update of submodule $name to current $SUBMODULE_BRANCH" 
     if test $SN1 -ne $SN2; then 
     git stash pop --quiet 
     fi 
     popd >/dev/null 
    fi 
    elif test $(git rev-parse HEAD) != "$sha1"; then 
    # No submodule.$name.branch for this submodule. Just checkout the 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; then 
    if test -n "$opt_init"; then 
    echo "Calling 'git submodule init'" 
    git submodule init 
    fi 
    # Make sure the submodules even exist. 
    echo "Calling 'git submodule update'" 
    git submodule update 
    # Call this script recursively for all 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 

「gitのサブモジュールの更新」と さえサポートと同じように行います。このスクリプトを呼び出します--initと--recursive。しかし、 'branch'値を設定することで、サブモジュールをチェックアウトし、代わりにブランチをプルするように設定することができます。 git config -f .gitmodules submodule.NAME.branch masterは、現在のSHA1が何であるかを問わず、サブモジュールNAMEにチェックアウトしてブランチマスタを引き出します。親プロジェクトがそのブランチのHEADを指し示すように更新されます。

関連する問題