何があったか

Hugo を使った自作ブログのリポジトリを別PCに git clone し、hugo server でローカル立ち上げを試みたが、真っ白なページしか出てこなかった。

原因を探ってみると、 themes の中身(「 hugo-coder 」を使っています)が空っぽだったので、git submodule add https://github.com/luizdepra/hugo-coder.git themes/hugo-coder で加えようとしたが、

'themes/hugo-coder' already exists in the index

と言われてしまい上手くいかなかった。

対策

どうやら git のキャッシュにすでに hugo-coder の記録があるらしく、それを削除しないと submodule add はできないとのことだった。1

> git ls-files --stage
︙
160000 1d4b612a4438ba6fcc2271da4e6c66749e8c9c57 0       themes/hugo-coder

あった

> git rm -r --cached themes/hugo-coder
rm 'themes/hugo-coder'

消せた

> git submodule add https://github.com/luizdepra/hugo-coder.git themes/hugo-coder
Cloning into 'C:/Hugo/Sites/blog-by-hugo/themes/hugo-coder'...
remote: Enumerating objects: 2200, done.
Remote: Total 2200 (delta 0), reused 0 (delta 0), pack-reused 2200 eceiving objects:  97% (2134/2200), 1.72 MiB | 1.69 MiB/s
Receiving objects: 100% (2200/2200), 4.22 MiB | 3.38 MiB/s, done.
Resolving deltas: 100% (1092/1092), done.

(2021/05/19 追記)

Hugo のブログテーマを変え、再び上記の状況になったが、上のやり方ではうまくいかなかった。

> git rm -r --cached themes/hugo_theme_pickles 
rm 'themes/hugo_theme_pickles'
> git submodule add https://github.com/mismith0227/hugo_theme_pickles.git themes/hugo_theme_pickles 
'themes/hugo_theme_pickles' already exists and is not a valid git repo
  • git submodule update --init が正解だったっぽい。2
~ git submodule update --init                                                                        
Submodule 'themes/hugo_theme_pickles' (https://github.com/mismith0227/hugo_theme_pickles.git) registered for path 'themes/hugo_theme_pickles'
Cloning into '/Users/kattsun/blog-by-hugo/themes/hugo_theme_pickles'...
Submodule path 'themes/hugo_theme_pickles': checked out '3e9a3c8fed75c753ad2f86facecd9c425c0ac6c2'

  1. 参考: Issue with adding common code as git submodule: “already exists in the index” - Stack Overflow ↩︎

  2. git submodule はトモダチ!怖くないよ! (チートシート付き) - エムスリーテックブログgit clone--recursiveを付けないとサブモジュールのファイルが取得されませんが、 git submodule update --init で後からチェックアウトできます。」 ↩︎