4
2

More than 3 years have passed since last update.

gitリポジトリの統合

Last updated at Posted at 2020-10-20

準備

統合先リポジトリ:main-repo
統合されるリポジトリ:sub-repo

統合編

【Git】2つのGitリポジトリを履歴を保持して統合したい

先駆者の方がいらっしゃいましたので同じ手順で行いました。

# 統合先リポジトリに移動
cd main-repo/

# 念の為ローカルリポジトリを最新にする
git pull

# 作業用のブランチを作成(ブランチ名:integration_repo)
git checkout -b "integration_repo"

# 統合するリポジトリを格納するためのディレクトリを作成
mkdir sub-dir

# 空ディレクトリをgit管理できるように.gitkeepを作成する
touch sub-dir/.gitkeep

# 一旦コミットする
git add -A
git commit -m "create sub-dir"

# 統合されるリポジトリを統合先リポジトリのリモートに追加する
git remote add sub-repo <統合されるリポジトリのURLかローカルリポジトリのPath>
git remote update sub-repo

# ちゃんとリモートに追加されているか確認
git remote -v
#=> origin <path> (fetch)
#=> origin <path> (push)
#=> sub-repo <path> (fetch)
#=> sub-repo <path> (push)

# 統合する
git merge --allow-unrelated-histories -X subtree=sub-dir sub-repo/master 

# コミットメッセージの入力画面が開く。そのままでよければそのまま保存する

# リモートリポジトリに反映
git push --set-upstream origin integration_repo

コマンドの意味

git remote add <remote> <url>

リモートを追加します。remoteはリモートに対する短縮名なのでわかりやすい名前ならなんでもいいです。urlにはローカルリポジトリをcloneしているならそのパスを指定することもできます。
この時、指定したurlのmasterが最新でないとその状態で統合されることになるのでgit remote updateする前にmasterがどのような状態であるか確認するのがベターです。

git remote update <remote>

リモートリポジトリからオブジェクトとそれに関連したメタデータを取得します。
git fetch <remote>でも代用可能です。

git merge --allow-unrelated-histories -X subtree=<path> <commit>

git merge <commit>・・・今いるブランチに<commit>の内容をマージします。
--allow-unrelated-histories・・・共通の祖先のいない履歴同士のマージを許可します。(デフォルトではこのようなマージは稀であり安全のため認められていません。)
-X subtree=<path>・・・マージ戦略のオプションを渡します。subtreeで指定した<path>が統合されるブランチのプレフィックスとして付与されたうえでマージされます
// subtreeを指定しないと、ルートディレクトリにマージされます。そうすると同じファイル名のものはもちろんマージされるので、競合の可能性もあり作業が複雑になります。

影響

Q 統合元のコミット履歴は統合先にも反映されるか

される

Q これ以降、統合元のコミットは統合先にも反映されるか

されない

Q これ以降、統合先のコミットは統合元にも反映されるか

されない

Q 統合元のMRとかPRは統合先に引き継がれるか

引き継がれない

Q 統合元のタグは引き継がれるか

引き継がれない

4
2
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
2