フォルダの中身が親に記録されない(adding embedded git repository)
やり直し: 注意手順を飛ばすと戻しにくくなります
- 作業中
- ステージ
- コミット後
- push 後
- マージ後
症状
カナ高2
theme を入れて git add . したら、長い注意が出た。ソラ高2
adding embedded git repository って?カナ高2
よそから持ってきた theme フォルダを tabi-shiori に入れ、git add . で次のコミットに入れる台(ステージ)に載せました。すると、長い注意が出ました。
warning: adding embedded git repository: theme
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.
hint: If you meant to add a submodule, use:
hint:
hint: git submodule add <url> theme
hint:
hint: If you added this path by mistake, you can remove it from the
hint: index with:
hint:
hint: git rm --cached theme
hint:
hint: See "git help submodule" for more information.
hint: Disable this message with "git config set advice.addEmbeddedRepo false"
このままコミットして push(送る)すると、ほかの人の手元では theme が空になります。
いまどの段階か
ステージの話です。ノートの間に別のノートが丸ごと挟まっていて、表紙のコミット ID だけが控えられている状態です。theme の中に .git があるので、Git は theme を別のリポジトリとみなします。.gitmodules が無いので、サブモジュールとしても使えません。
図: いまの状態 → 直した後の状態
- オレンジの輪。そのコマで変わった所です。
こうなるまで全 3 コマ
-
1 / 3 はじめ
はじめは、shiori.md だけが記録されています。
-
2 / 3 あなた
git clone <URL> theme別のリポジトリを theme というフォルダに置きました。theme の中には、それ自身の .git があります。
-
3 / 3 あなた
git add .親には theme のコミット ID だけが記録されました。style.css の中身は入っていません。
直すとき全 4 コマ
-
1 / 4 いま
親に記録されたのは theme のコミット ID だけ。style.css は入っていない。
-
2 / 4 あなた
rm -rf theme/.gittheme の中の .git を消しました。theme は、ただのフォルダになります。
-
3 / 4 あなた
git rm --cached theme親に記録されていた theme の札(コミット ID)を外しました。ファイルは消えません。
-
4 / 4 あなた
git add themetheme を入れ直すと、中の style.css がステージに載りました。
まず確かめる
$ git ls-tree HEAD
100644 blob b4785957bc986dc39c629de9fac9df46972c00fc shiori.md
160000 commit 950b4759fbdc5e6424020a70ea4e8059bac37e81 theme
$ git submodule status
fatal: no submodule mapping found in .gitmodules for path 'theme'
theme が 160000 commit になっていたら、中身が入っていない印です。まだコミットしていなければ git status で new file: theme と 1 行だけ出ます。
直し方
theme の履歴は要らない、中のファイルを親で管理したい場合です。
theme の中の .git を消して、親で管理する
コマンド
$ rm -rf theme/.git
$ git rm --cached theme
rm 'theme'
$ git add theme
$ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: theme
new file: theme/style.cssVS Code
ソース管理では theme が 1 行だけ並び、中のファイルが出ません。VS Code にボタンが無いので、ターミナル(⌃` で開く)で打つと、theme の中のファイルが 1 つずつ並ぶようになります。
コミットすると、git ls-tree -r HEAD --name-only に theme/style.css が並びます。
theme を別のリポジトリのまま使いたいなら、git rm --cached theme の後、git submodule add <URL> theme で入れ直します。
やってはいけないこと
rm -rf theme/.git は theme 自身の履歴を消します。theme で自分がコミットしてきた記録が要るなら、先に push するか、サブモジュールにするほうを選びます。
advice.addEmbeddedRepo で注意を消すのは、意味がわかってからにします。消しても中身が記録されないことは変わりません。