LoginSignup
6
5

More than 3 years have passed since last update.

[git] 複数のリモートリポジトリを扱う&サブモジュール&複数のリモートリポジトリのサブモジュール

Posted at

複数のリモートリポジトリを扱うための説明です.
さらにサブモジュールの扱い方も説明し,サブモジュールのリモートリポジトリを複数にする方法までまとめます.

GitHub+GitLabなどにしてサーバーダウンに備えましょう!

複数のリモートリポジトリを登録,同時にpushする

GitHub+GitLabとか,サーバーダウン対策にもなると思います.次のコマンドを1回だけ操作すると.git/configが追加されます.

git remote set-url origin --add (2つめのリモートリポジトリのurl)

fetchしてくるリポジトリ

上記の方法でやると,.git/config

[remote "origin"]
    url = git@(1つめのリモートリポジトリのurl):username/hoge.git
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = git@(2つめのリモートリポジトリのurl):username/hoge.git

のようになるが,この順番に注意.

$ git remote -v
origin  git@(1つめのリモートリポジトリのurl):username/hoge.git (fetch)
origin  git@(1つめのリモートリポジトリのurl):username/hoge.git (push)
origin  git@(2つめのリモートリポジトリのurl):username/hoge.git (push)

1つめの方をfetchしてくるようになっている.remote-tracking branchの設定に起因?

リポジトリ内に別のリポジトリ

リポジトリ内で別のリポジトリを管理したいとき.特定のcommitを参照できます.

  • リポジトリにサブモジュールを追加する
git submodule add (リモートリポジトリのurl) (必要ならディレクトリ名)

.gitmodules にそのサブモジュールの情報が入ります.

別のローカルリポジトリで,追加済みのサブモジュールを追加する

git submodule update -i
  • リポジトリ内のサブモジュールを全てmergeする 大元のリポジトリでgit pullするとサブモジュールのリポジトリもfetchのみされるので,それを全て一括mergeしたい場合
git submodule foreach git merge

- サブモジュールを最新のブランチにする(全てgit pullする)

git submodule foreach git pull origin master

(参考)
-- https://qiita.com/sotarok/items/0d525e568a6088f6f6bb
-- https://qiita.com/kinpira/items/3309eb2e5a9a422199e9

submoduleで複数のリモートリポジトリ

同様にsubmoduleのリポジトリ内で

git remote set-url origin --add (2つめのリモートリポジトリのurl)

してやればよい.(大元のリポジトリの).git/modules/hoge/config.git/configと同様の情報がある.

6
5
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
6
5