Git サブモジュールのリモート URL の変更とリポジトリ内の同期

Git 管理している作業ディレクトリに、外部のリポジトリをサブモジュールとして配置してから、そのサブモジュールのリモート URL設定するファイル変更して、それをローカルのリポジトリ内で同期するコマンド実行と結果を紹介します。

git-scm 公式サイトの一部 20250408

Git-2.49.0 (64 bit) を使用します。

例として、Gut をサブモジュールとして配置した後、そのサブモジュールの一部を変更して使いたいためにフォークしたリポジトリにリモート URL を変更します。

Git サブモジュールのリモート URL の変更とリポジトリ内の同期2

.gitmodules ファイルのリモート URL を変更

リポジトリに配置した Git サブモジュール(例では third_party/Gut) のリモート URL を変更して、他のリポジトリ(例では Gut をフォークした自身のリポジトリの Gut)に clone, fetch する場所を変える場合は、 .gitmodules ファイルの該当するセクション(例では submodule “third_party/Gut”)の url を変更します。

[submodule "third_party/Gut"]
	path = third_party/Gut
	url = https://github.com/bitwes/Gut.git

リモート URL の .git の場所は、ウェブサービス GitHub に公開しているリポジトリならば、そのリポジトリのページを開いて Code ボタンを押すと表示される .git のパスの右側のコピーボタンを押して取得します。

Git サブモジュールのリモート URL の変更1

以下は url を、 Gut をフォークした自身の管理する Gut リポジトリに変更した結果です。

[submodule "third_party/Gut"]
	path = third_party/Gut
	url = https://github.com/sakura-crowd/Gut.git

.gitmodules ファイルの設定をリポジトリ内で同期する

.gitmodules の変更をローカルのリポジトリの設定に同期します。

git submodule sync は、リポジトリのサブモジュールのリモート URL 構成設定を、.gitmodules で指定された値に同期します。

Inspects, updates and manages submodules.
For more information about submodules, see gitsubmodules[7].

サブモジュールを検査、更新、管理します。
サブモジュールの詳細については、gitsubmodules[7] を参照してください。

sync [–recursive] [–] [<path>…​]

Synchronizes submodules’ remote URL configuration setting to the value specified in .gitmodules. It will only affect those submodules which already have a URL entry in .git/config (that is the case when they are initialized or freshly added). This is useful when submodule URLs change upstream and you need to update your local repositories accordingly.

git submodule sync synchronizes all submodules while git submodule sync -- A synchronizes submodule “A” only.

If --recursive is specified, this command will recurse into the registered submodules, and sync any nested submodules within.

サブモジュールのリモート URL 構成設定を、.gitmodules で指定された値に同期します。これは、.git/config に URL エントリがすでに存在するサブモジュールにのみ影響します (初期化または新たに追加された場合)。これは、サブモジュールの URL が上流で変更され、それに応じてローカル リポジトリを更新する必要がある場合に便利です。

git submodule sync はすべてのサブモジュールを同期しますが、git submodule sync — A はサブモジュール「A」のみを同期します。

–recursive が指定されている場合、このコマンドは登録されたサブモジュールを再帰的に実行し、その中にネストされたサブモジュールがあれば同期します。

Git – git-submodule Documentation と Google 翻訳

リポジトリの作業ディレクトリの最上位で git submodule sync コマンドを実行します。

G:\Dev\Godot4GD\SakuraCrowd\SakuraCrowdGodotLib>git submodule sync
Synchronizing submodule url for 'third_party/Gut'

G:\Dev\Godot4GD\SakuraCrowd\SakuraCrowdGodotLib>

リポジトリの設定との同期が行われたサブモジュールの path (例では ‘third_party/Gut’ ) が表示されました。

リポジトリの .git/config ファイルを確認すると、実行前と実行後で url が変わっています。

[submodule "third_party/Gut"]
	url = https://github.com/bitwes/Gut.git
	active = true
[submodule "third_party/Gut"]
	url = https://github.com/sakura-crowd/Gut.git
	active = true

また、サブモジュール(例では third_party/Gut)のローカルのリポジトリ(例では .git/modules/third_party/Gut)の config ファイル同じく変更されて、サブモジュールの fetch 先も変更されます。

[core]
	repositoryformatversion = 0
	filemode = false
	bare = false
	logallrefupdates = true
	symlinks = false
	ignorecase = true
	worktree = ../../../../third_party/Gut
[remote "origin"]
	url = https://github.com/sakura-crowd/Gut.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
	remote = origin
	merge = refs/heads/main

.gitmodules を変更しない一時的なリモート URL の変更のコマンド

.gitmodules を変更せずに、サブモジュールの push, fetch のリモート URL をローカルで一時的に変更したい場合は、git remote set-url を使用します。

使用する場合は、そのサブモジュール(例えば先ほどの third_party/Gut)のディレクトリに移動して git remote set-url を実行します。
※実際に行っていないため、これについては、以下の引用元を参照してください。

Manage the set of repositories (“remotes”) whose branches you track.

ブランチを追跡するリポジトリ (「リモート」) のセットを管理します。

set-url

Changes URLs for the remote. Sets first URL for remote <name> that matches regex <oldurl> (first URL if no <oldurl> is given) to <newurl>. If <oldurl> doesn’t match any URL, an error occurs and nothing is changed.

With --push, push URLs are manipulated instead of fetch URLs.

With --add, instead of changing existing URLs, new URL is added.

With --delete, instead of changing existing URLs, all URLs matching regex <URL> are deleted for remote <name>. Trying to delete all non-push URLs is an error.

Note that the push URL and the fetch URL, even though they can be set differently, must still refer to the same place. What you pushed to the push URL should be what you would see if you immediately fetched from the fetch URL. If you are trying to fetch from one place (e.g. your upstream) and push to another (e.g. your publishing repository), use two separate remotes.

リモートの URL を変更します。正規表現 に一致するリモート の最初の URL ( が指定されていない場合は最初の URL) を に設定します。 がどの URL とも一致しない場合、エラーが発生し、何も変更されません。

–push を使用すると、URL をフェッチする代わりにプッシュ URL が操作されます。

–add を使用すると、既存の URL を変更する代わりに、新しい URL が追加されます。

–delete を使用すると、既存の URL を変更する代わりに、正規表現 に一致するすべての URL がリモート から削除されます。非プッシュ URL をすべて削除しようとするとエラーになります。

プッシュ URL とフェッチ URL は、異なるように設定できても、同じ場所を参照する必要があることに注意してください。プッシュ URL にプッシュしたものは、フェッチ URL からすぐにフェッチした場合に表示されるものである必要があります。ある場所 (アップストリームなど) からフェッチし、別の場所 (公開リポジトリなど) にプッシュしようとしている場合は、2 つの別々のリモートを使用します。

Git – git-remote Documentation と Google 翻訳

まとめ

今回は、Git 管理している作業ディレクトリに、外部のリポジトリをサブモジュールとして配置してから、そのサブモジュールのリモート URL設定するファイル変更して、それをローカルのリポジトリ内で同期するコマンド実行と結果を紹介しました。

参照サイト Thank You!

記事一覧 → Compota-Soft-Press

コメント

Ads Blocker Image Powered by Code Help Pro

お願い - Ads Blocker Detected

このサイトは広告を掲載して運営しています。

ポップアップを閉じて閲覧できますが、よろしければ

このサイト内の広告を非表示にする拡張機能をオフにしていただけませんか?

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

タイトルとURLをコピーしました