LoginSignup
17
15

More than 5 years have passed since last update.

Git リポジトリを分割する

Posted at

以下のようなディレクトリ構造のリポジトリを分割する方法を場合分けしてまとめます。

repo1/
 ├─ subdir/
 ├─ aaa
 ├─ bbb
 ├─ ccc
 └─ ddd

ケース1:サブディレクトリを切り出す

リポジトリ repo1 のサブディレクトリ subdir を別のリポジトリ(のルートディレクトリ)として切り出したい場合:

git filter-branch --subdirectory-filter subdir HEAD

ケース2:ディレクトリ構造を保持して一部を切り出す

リポジトリ repo1 から aaa, bbb を切り出したい場合,ccc, ddd, subdir を削除して歴史修正します。

git filter-branch --tree-filter 'rm -rf ccc ddd subdir' --prune-empty
git gc

git filter-branch での変更を複数回しようとすると,以下のエラーが出るので,-f オプションを付けます。

Cannot create a new backup.
A previous backup already exists in refs/original/
Force overwriting the backup with -f

補足:リモートリポジトリを追加して push する

git remote add new_origin {URL}
git push new_origin

参考ページ

17
15
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
17
15