git submoduleで共有モジュールを別リポジトリ管理する – CodeCommit

git submoduleで共有モジュールを別リポジトリ管理する – CodeCommit

プロジェクトごとにリポジトリを作成し、複数プロジェクトでモジュールを共有したい場合にgit submodule機能を使います。

svnでいうexternal(外部参照)に当たる機能だと思います。

共有モジュール用のリポジトリ作成

commonというリポジトリ作成します。(https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/common)

git submoduleで共有モジュールを別リポジトリ管理する

git submoduleで共有モジュールを別リポジトリ管理する

共有モジュールをプッシュします。

$ git add .
$ git commit -m "commit"
$ git push origin master

git submoduleで共有モジュールを別リポジトリ管理する

git submodule作成

git submodule addコマンドでサブモジュールを作成します。

各プロジェクト直下で実行します。(ここではa-project)

git submodule作成

$ git submodule add \
  https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/common common
$ git submodule
 b22f312e190de8a728e659bb01a8d1981613f325 common (heads/master)

git submoduleで共有モジュールを別リポジトリ管理する

.gitmodulesファイルが作成されます。

[submodule "common"]
    path = common
    url = https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/common
$ git add .
$ git commit -m "commit"
$ git push origin master

CodeCommitは以下のようになります。

git submoduleで共有モジュールを別リポジトリ管理する - CodeCommit

各プロジェクトでgit submoduleを作成すれば、各プロジェクトからcommonを共有することができます。

共有モジュールの修正

a-project,b-projectでcommonを共有しているとします。

a-project\commonの共有モジュールの修正があった場合は、カレントディレクトリをcommonにします。

$ cd common
$ vi log.js
~ファイル修正
$ git add .
$ git commit -m "commit"
$ git push origin master

これでcommonリポジトリにpushされます。

b-project\commonはpullする必要があります。

$ cd common
$ git pull origin master
git: 'credential-manager' is not a git command. See 'git --help'.

The most similar command is
        credential-manager-core
From https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/common
 * branch            master     -> FETCH_HEAD
Updating 4b7a908..cb05c78
Fast-forward
 log.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

プロジェクトルートからの場合、以下コマンド実行します。

$ git submodule update --remote

サブモジュール作成済みのリポジトリをclone

$ git clone https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/a-project

common配下が空になっているので、submoduleをチェックアウトします。プロジェクトルートで以下コマンド実行します。

$ git submodule update --init --recursive

git submodule解除

git submoduleの解除は以下コマンドで実行します。

このコマンドでsubmoduleのリポジトリ内のソースが削除されるわけではありません。あくまでも紐づけの解除になります。

$ git submodule deinit common
$ git rm common
$ rm -rf  .git/modules/common

これで紐づけが解除されます。

コメント

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