無料のバージョン管理ソフト Git で、リポジトリ内に誤って追加したサブモジュールを削除するため、git-reset –hard コマンドでサブモジュールの追加前にリセットしてもローカルリポジトリに残っている .git\ フォルダ内のサブモジュールの設定とサブモジュールのリポジトリを削除した際の手順を紹介します。
git config コマンドで .git\config ファイル以外の設定を変更するオプションについても紹介します。

※Git-2.49.0 (64 bit) を使用します。
git reset –hard をしてもサブモジュールの設定が残りました
Git のサブモジュールを使うことで、リポジトリ内に外部のリポジトリを区別して配置できます。
サブモジュールを使うと、ある Git リポジトリを別の Git リポジトリのサブディレクトリとして扱うことができるようになります。 これで、別のリポジトリをプロジェクト内にクローンしても自分のコミットは別管理とすることができるようになります。
Git – サブモジュール
誤って git submdule add コマンドでサブモジュールをローカルリポジトリに追加してしまいました。
add [-b ] [-f|–force] [–name ] [–reference ] [–ref-format ] [–depth ] [–] []
Add the given repository as a submodule at the given path to the changeset to be committed next to the current project: the current project is termed the “superproject”.指定されたリポジトリを、現在のプロジェクトの次にコミットされる変更セットへの指定されたパスにあるサブモジュールとして追加します。現在のプロジェクトは「スーパープロジェクト」と呼ばれます。
Git – git-submodule Documentation と Google 翻訳
そこで、 git reset –hard コマンドを使って、ローカルリポジトリを、誤ったサブモジュールを追加する前の状態に戻しました。
In the first three forms, copy entries from to the index. In the last form, set the current branch head (HEAD) to , optionally modifying index and working tree to match. The / defaults to HEAD in all forms.
最初の 3 つの形式では、エントリをインデックスにコピーします。最後の形式では、現在のブランチ ヘッド (HEAD) を に設定し、オプションでインデックスと作業ツリーを一致するように変更します。 / は、すべての形式でデフォルトで HEAD になります。
Git – git-reset Documentation と Google 翻訳
これにより、.gitmodules ファイルは、誤ったサブモジュールの設定を追加する前の状態に戻りました。
.gitmodules には、不要なサブモジュールを追加する前の、 third_party/Gut フォルダに配置したサブモジュールの設定だけが残されています。
[submodule "third_party/Gut"]
path = third_party/Gut
url = https://github.com/sakura-crowd/Gut.git
しかし、ローカルの .git\config ファイルには、その誤って追加したサブモジュールの設定がリセットされずに残っています。
の削除の手順1.png)
[submodule "addons/godot-git-plugin"]
url = https://github.com/godotengine/godot-git-plugin.git
active = true
[submodule "addons/Gut"]
url = https://github.com/bitwes/Gut.git
active = true
[submodule "addons/third_party/Gut"]
url = https://github.com/bitwes/Gut.git
active = true
[submodule "third_party/Gut"]
url = https://github.com/bitwes/Gut.git
active = true
また、 .git\modules\ フォルダにも、誤って追加したサブモジュールのリポジトリが残されています。
の削除の手順4.png)
今回は、git reset –hard でをリセットした後も残っている、 .git\ フォルダ内の不要なサブモジュールを削除します。
変更前に念のためバックアップ
念のため変更される .git\config と .gitmodules ファイルをコピーして別の場所に退避しておきます。
の削除の手順2.png)
git config で不要なサブモジュールの設定を削除
.git\config ファイルに残っている不要なサブモジュールのセクションを git config –remove-section コマンドで削除します。
You can query/set/replace/unset options with this command. The name is actually the section and the key separated by a dot, and the value will be escaped.
このコマンドを使用して、オプションを照会/設定/置換/設定解除できます。名前は実際にはドットで区切られたセクションとキーであり、値はエスケープされます。
–remove-section <name>
Replaced by
git
config
remove-section
<name>.git config remove-section に置き換えられます。
Git – git-config Documentation と Google 翻訳
.git\config ファイルのサブモジュールのセクションを削除するには以下のようにサブモジュールを配置しているフォルダのパスを指定します。
git config --remove-section submodule.サブモジュールのフォルダの相対パス
例えば、ローカルリポジトリの addons\Gut フォルダに配置したサブモジュールのセクションを削除するには、以下のように指定します。
git config --remove-section submodule.addons/Gut
以下のコマンドプロンプトの入力では、3つの addons フォルダ内に配置したサブモジュールのセクションを削除しています。
※ Windows 10 では、フォルダを開いたエクスプローラのアドレスバーに cmd⏎ と入力すると、そのフォルダを作業ディレクトリにしてコマンドプロンプトが起動します。
G:\Dev\Godot4GD\SakuraCrowd\SakuraCrowdGodotLib>git config --remove-section submodule.addons/godot-git-plugin
G:\Dev\Godot4GD\SakuraCrowd\SakuraCrowdGodotLib>git config --remove-section submodule.addons/Gut
G:\Dev\Godot4GD\SakuraCrowd\SakuraCrowdGodotLib>git config --remove-section submodule.addons/third_party/Gut
G:\Dev\Godot4GD\SakuraCrowd\SakuraCrowdGodotLib>
の削除の手順3.png)
指定した3つのサブモジュールのセクションは削除され、それ以外のサブモジュールのセクションが .git\config ファイルに残されました。
[submodule "third_party/Gut"]
url = https://github.com/bitwes/Gut.git
active = true
git config で .git\config ファイル以外を変更する
今回は不要な設定がなかったために行いませんでしたが、git config コマンドで .git\config 以外のファイルを、–file オプションでファイル名を指定することで編集できます。
例えば、.gitmodules ファイルの [submodule “addons/Gut”] セクションを削除したい場合は、先ほどの .git/config のセクション指定に加えて、 –file .gitmodules を追加して .gitmodules ファイルに対してセクションの削除を行います。
git config --file .gitmodules --remove-section submodule.addons/Gut
-f <config-file>
–file <config-file>For writing options: write to the specified file rather than the repository
.git/config
.
For reading options: read only from the specified file rather than from all available files.
See also FILES.書き込みオプションの場合: リポジトリ .git/config ではなく、指定されたファイルに書き込みます。
Git – git-config Documentation と Google 翻訳
読み取りオプションの場合: 使用可能なすべてのファイルではなく、指定されたファイルからのみ読み取ります。
.git\modules 内のサブモジュール用のローカルリポジトリを削除
サブモジュールのローカルリポジトリも .git\config ファイルの設定と同様に、ローカルリポジトリの .git\modules\ フォルダ内に残されています。
# おそらく、作業ディレクトリのサブモジュールの差分などを管理するために .git\modules 内にサブモジュール用のリポジトリを作成したものが、残されていると思います。
の削除の手順4.png)
相当古いバージョンの Git でなければ、サブモジュール関連の Git データはメインプロジェクトの .git ディレクトリに保存されます。古いバージョンを使っていなければ、サブモジュール用ディレクトリを削除してもコミットやブランチのデータは残ったままです。
Git – サブモジュール
サブモジュールのリポジトリは、 .git\modules\ フォルダ内に、サブモジュールを配置したフォルダのパスと同様に配置されています。
例えば、 addons\Gut に配置したサブモジュールのリポジトリは .git\modules\addons\Gut に配置されています。
これも、git reset –hard によるリセットでは削除されなかったため、エクスプローラ上などで手動で不要なサブモジュールのリポジトリのフォルダを削除します。
※問題はありませんでしたが、公式ガイドは見つからなかったため、自己責任で行ってください。
の削除の手順5.png)
まとめ
今回は、無料のバージョン管理ソフト Git で、リポジトリ内に誤って追加したサブモジュールを削除するため、git-reset –hard コマンドでサブモジュールの追加前にリセットしてもローカルリポジトリに残っている .git\ フォルダ内のサブモジュールの設定とサブモジュールのリポジトリを削除した際の手順を紹介しました。
git config コマンドで .git\config ファイル以外の設定を変更するオプションについても紹介しました。
参照サイト Thank You!
- Git
- Git – サブモジュール
- Git – git-submodule Documentation
- Git – git-reset Documentation
- Git – git-config Documentation
- TortoiseGit – Windows Shell Interface to Git
記事一覧 → Compota-Soft-Press
コメント