0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

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

Posted at

はじめに

本記事は、自分用の覚書として作成したものです。が、皆さんのお役に立てれば幸いです。

注意事項

複数のリポジトリを一つにまとめると、例えば以下で説明する統合元のディレクトリ docs/meisai 以下に置かれているファイルやディレクトリは、統合先のディレクトリ Documents 以下に置かれることになります。

  • 例えば、docs/meisai/Makefie は、Documents/meisai/Makefile ではなくDocuments/makefile に置かれます。
  • 統合元・統合先それぞれのディレクトリ以下に同名のファイルやディレクトリが存在する場合には、それを考慮して統合する必要があるのではないかと思います。
  • 私の場合、そのようなことはなかったので、もしそのような状況になった場合に git merge を実行してしまうとどのようなことになるのかは、残念ながらわかりません。

リポジトリの構成

今回は、docs 以下の三つのリポジトリ (2023/meisai, meisai, report) を新たに作成したリポジトリ (Documents) に統合する方法を解説します。

  • docs と Documents は、いずれもホームディレクトリの直下に置かれていることを想定しています。

リポジトリ (ディレクトリ) の構成:

docs -+- 2023/meisai/.git
      +- meisai/.git
      +- report/.git

新しくディレクトリ Documents を作成し、git init を実行し、適当なファイル (README.md) を作成 git add; git commit した後に docs 以下のリポジトリを Documents 以下に取り込みます。

  • README.md の作成、git add; git commit はやらなくても構いません。
  • 既に存在するリポジトリに docs 以下のリポジトリを取り込むことを想定して、README.md の作成、git add; git commit しただけなので。

ホームディレクトでの操作手順を示します。

$ mkdir ~/Documents
$ cd ~/Documents
$ git init
$ touch README.md
$ git add README.md
$ git commit

以降の操作は、引き続き Documents ディレクトリで実行してください。

Documents に git remote コマンドを使用して docs 以下のリポジトリを登録します。

$ cd ~/Documents
$ git remote add 2023_repo ../docs/2023/meisai
$ git remote add meisai_repo ../docs/meisai
$ git remote add report_repo ../docs/report
$ git remote
2023_repo
meisai_repo
report_repo
$ 

add の直後に指定する 2023_repo など _repo は、分かりやすくするために _repo を付与しているだけなので、2023 などでも問題ないはずです。

登録したリポジトリを Documents に取り込みます。

report_repo
$ git fetch 2023_repo
$ git fetch meisai_repo
$ git fetch report_repo

Documents にマージします。

  • Git 2.9 意向を使用する場合には、merge の後に --allow-unrelated-histories を指定してください。
$ git merge 2023_repo/master
$ git merge meisai_repo/master
$ git merge report_repo/master

以上です。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?