LoginSignup
4
3

More than 3 years have passed since last update.

gitを入れ子にする(submodule)

Posted at

gitを入れ子にする(submodule)

gitレポジトリを入れ子にするにはsubmoduleという機能を使う。

参考

やりたいこと

下記のツリー図において、以下の2点を実施したい

  • module_Cをproj_Aと異なるレポジトリとして分離したい
  • 分離したmodule_Cをproj_B内に追加したい
.
├── proj_A
│   ├── A.py
│   └── module_C ★これをproj_Aから分離したい
│       └── C.py
└── proj_B
    ├── B.py
    └── ★ここにmodule_Cを追加したい

module_Cをproj_Aと異なるレポジトリとして分離する

module_Cをproj_A外のどこかにコピーする
分かりやすくするため、名前はレポジトリに格納する名称に変更しておく。

$ cd proj_A
$ cp -r module_C ../repos_C

githubにrepos_C格納用の空レポジトリ(仮名:repos_C)を作っておいて
コピーしたフォルダを通常の手順でgithubに登録する

$ cd ../repos_C
$ echo "# repos_C" >> README.md
$ git init
$ git add README.md
$ git commit -m "first commit"
$ git remote add origin https://github.com/username/repos_C.git
$ git push -u origin master

proj_Aのレポジトリからmodule_Cを削除

$ cd proj_A
$ rm -rf module_C
$ git add -A
$ git commit

repos_Cを改めてcloneする

$ git submodule add git@github.com:username/repos_C.git

git@github.com:は状況にあわせてhttps://github.com/とかに適宜変更する

あとはgit commitして完了

分離したmodule_Cをproj_B内に追加したい

手順は先のproj_Arepos_Cを追加したのと同じ手順でOK

$ cd proj_B
$ git submodule add git@github.com:username/repos_C.git

各ディレクトリが紐づいてるレポジトリを確認する

実際にどのレポジトリに紐づいているかを確認するにはgit remote -vを使う

proj_Aの確認(proj_Bも同様)

$ cd proj_A
$ git remote -v
origin  https://github.com/username/proj_A.git (fetch)
origin  https://github.com/username/proj_A.git (push)

その中のrepos_Cの確認

$ cd repos_C
$ git remote -v
origin  https://github.com/username/repos_C.git (fetch)
origin  https://github.com/username/repos_C.git (push)
4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3