LoginSignup
531
511

More than 5 years have passed since last update.

Git で複数のリポジトリをまとめたり、逆に切り出したりする

Last updated at Posted at 2012-12-26

~/repo1 と ~/repo2 をまとめる

2つのリポジトリをまとめて1つのリポジトリにしたいとき。

cd ~/repo1
git remote add repo2 ~/repo2
git fetch repo2

git merge repo2/master

~/repo1/subdir に ~/repo2 を入れる

あるリポジトリのサブディレクトリに別のリポジトリの中身を入れたいとき。たとえば、あるリポジトリのサブディレクトリを切り出して別のリポジトリとして管理しているものを、元のリポジトリに合流させたいとき。

cd ~/repo1
git remote add repo2 ~/repo2
git fetch repo2

# サブディレクトリの内容に repo2 の内容をマージする
# (repo2 と内容が似ているサブディレクトリを自動で判別)
git merge -s subtree repo2/master

# ↑でうまくいかないときにはパスを指定する↓
git merge -X subtree=subdir repo2/master

# そもそも ~/repo1/subdir が存在しないときには↓
git read-tree --prefix=subdir/ repo2/master
git checkout -- .

~/repo1/dir1 と ~/repo1/dir2 をマージする

(タイトルからは外れるが)同一リポジトリ内の2つのサブディレクトリをマージしたいとき:http://qiita.com/uasi/items/545665066619d8cb8dbe

~/repo1/subdir を ~/repo2 として切り出す

あるリポジトリのサブディレクトリを別のリポジトリとして管理したいとき。

git clone ~/repo1 ~/repo2
cd ~/repo2
git filter-branch --subdirectory-filter subdir HEAD

余談:ここで git clone の代わりに git-new-workdir を使ってはいけない(コメント参照

参考にしたページ:

531
511
7

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
531
511