46
29

More than 5 years have passed since last update.

複数のGitリポジトリを一つにまとめる

Posted at

また例によって他の方の例の通りにやっても自分の希望通りにはできなかったので、自分なりの試行錯誤も込めて。

目的

  • 複数のGitリポジトリを一つにまとめたい。
  • まとめた後、元のリポジトリは削除し統合したリポジトリで運用したい。
  • ローカルリポジトリ間で統合させたい(いろいろ試行錯誤したかったため)
  • ディレクトリ構成も綺麗にしておきたい。

参考の方法ではディレクトリ構成が希望通りに行かなかったり、
ディレクトリ構成は上手くいってもリポジトリが希望通りにマージされなかったりしたので
その辺りを対処した手法になります。

構成

parent_repo(親リポジトリ、新規に作成)
├child_repo1
├child_repo2
└child_repo3

Finder上で見るとまとめた後は以下のような構成になります。

スクリーンショット 2018-12-28 10.10.04.png

作業の前に

元のリポジトリにあるブランチはmasterにマージしておくなど、綺麗な状態にしておくと後々混乱しなくて良いかもです。

手順

親となるリポジトリ作成

console
$ git init
$ git add .
$ git commit -m "first commit"
[master (root-commit) cad08ec] first commit
 2 files changed, 3 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 README.md

子のリポジトリを統合

git checkoutした後で一度commitしないとマージできないので注意。

console
$ git remote add child_repo1 /Users/miutex/Desktop/child_repo1
$ git fetch child_repo1
warning: no common commits
remote: Counting objects: 16, done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 16 (delta 4), reused 0 (delta 0)
Unpacking objects: 100% (16/16), done.
From /Users/miutex/Desktop/child_repo1
 * [new branch]      develop        -> child_repo1/develop
 * [new branch]      feat/func1 -> child_repo1/feat/func1
 * [new branch]      master         -> child_repo1/master
 * [new tag]         1.1.0 -> 1.1.0
$ git read-tree --prefix=child_repo1/ child_repo1/master
$ git checkout -- .
$ git add .
$ git commit -m "add child_repo1"
[master 2ef305c] add child_repo1
 1 file changed, 330 insertions(+)
 create mode 100644 child_repo1/index.js
$ git merge -s subtree child_repo1/master --allow-unrelated-histories
Merge made by the 'subtree' strategy.

リモートリポジトリを削除

上記「子のリポジトリを統合」の1行目で行ったローカルパスをリモートリポジトリとして登録したものを削除します。
ターミナルでやるでも、SourceTree等のツールでやるでも可。私はSourceTree上で削除しました。
(重要)リモートリポジトリを削除するとコミットメッセージ、タグは残りますがブランチ名は消えます。

結果

SourceTreeで見ると以下のような形に統合されます。

スクリーンショット 2018-12-28 10.20.24.png

参考

46
29
1

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
46
29