0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

empty submoduleを(reset等の後で)複数回remote addする

Last updated at Posted at 2019-12-14

resetやブランチを移動するなどして、2回、git submodule addすると、If you want to reuse this local git directory instead of cloning again from (snip) use the '--force' optionなるエラーとなり、submoduleの追加に失敗する。
指定通りに--forceすればよい。以下のbashで検証できる。

function make_submodule() {
git submodule add -f https://github.com/cielavenir/submodule-child-readme.git
}

git clone https://github.com/cielavenir/submodule-test
cd submodule-test
git config user.name cielavenir
git config user.email cielartisan@gmail.com

make_submodule
git submodule

git reset --hard
rm -rf .gitmodules *
git checkout .

make_submodule
git submodule

しかし、submodule上にmirrorを作りたかった場合など、empty submoduleの場合は事情が違ってくる。.gitmodulesを力技で書く必要がある他、--forceに加え当該submoduleをaddする必要があるらしい。
以下のbashで検証できる。

function make_submodule() {
tab=$'\t'
if [ ! -d submodule-child ]; then
    git submodule add -b master -f https://github.com/cielavenir/submodule-child.git
    if [ ! -f submodule-child/.git ]; then
        echo '[-] need to write .git manually'
        echo "gitdir: ../.git/modules/submodule-child" > submodule-child/.git
    fi
    git add submodule-child
    cat << EOM >> .gitmodules
[submodule "submodule-child"]
${tab}path = submodule-child
${tab}url = git@github.com:cielavenir/submodule-child.git
EOM
fi
git add .gitmodules
}

git clone https://github.com/cielavenir/submodule-test
cd submodule-test
git config user.name cielavenir
git config user.email cielartisan@gmail.com

make_submodule
touch submodule-child/readme
git -C submodule-child add readme
git -C submodule-child commit -m 'initial'
git add submodule-child

git submodule

git reset --hard
rm -rf .gitmodules *
git checkout .

make_submodule
git submodule
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?