LoginSignup
44
41

More than 5 years have passed since last update.

gitの特定のディレクトリをブランチ毎別リポジトリにする方法

Last updated at Posted at 2013-08-16

baseprojectという名前のリポジトリの中にあるsubprojectというディレクトリを、別のリポジトリとして分離する方法です。
その際、必要なブランチも全部移行します。

まず新しく作るリポジトリ用のディレクトリを作ります。

mkdir ~/subproject

新しく作ったディレクトリに元のプロジェクトをcloneしてやります。

cd ~/
git clone baseproject subproject

git filter-branch --subdirectory-filterを使えば指定したディレクトリに関する情報だけ抽出されます。
-- --allをつけると全てのブランチ情報も一緒に抽出されます。

cd ~/subproject/
git filter-branch --subdirectory-filter subproject -- --all

元のブランチがリモートブランチとして移行できたかを確認します。

git branch -a

移行する必要があるブランチをあらかじめcheckoutしておきます。

git checkout -b origin/feature001
git checkout -b origin/feature002

あとは全部pushするだけ!

git remote rm origin
git remote add origin git@github.com:hoge/subproject.git
git push --all
44
41
2

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
44
41