LoginSignup
16
14

More than 5 years have passed since last update.

gitレポジトリ内のディレクトリを別のgitレポジトリ以下に移動する

Last updated at Posted at 2017-02-21

具体的には

repo-A/dir1/dir2/dir3

repo-B/dir4/
以下に入れたい。
つまり、
repo-B/dir4/dir3
としたい。

repo-Aの履歴を書き換え、移行後の形に整える

cd /tmp
git clone https://github.com/repo-A
cd repo-A/
git filter-branch -f --subdirectory-filter dir1/dir2/dir3/ -- --all # dir3以外のファイルを消し、dir3以下のファイルがrepo-A以下に展開される。
git filter-branch -f --tree-filter "mkdir dir3 && git mv -k * dir3/" # 履歴の書き換え。dir3ディレクトリを作って、mv。
git filter-branch -f --tree-filter "mkdir dir4 && git mv -k * dir4/" # さらに成型。dir4を作って、mv

lsして、repo-A以下が移行したいフォルダ構成になっているか確認.

/tmp/repo-A/dir4/dir3
 $ ls
file1 file2

作った(移行したい)レポジトリ(repo-A)を、移動先(repo-B)のリモートに登録する。

cd tmp
git clone https://github.com/repo-B
cd repo-B
git remote add work file:///tmp/repo-A

リモートの確認.

git remote -v
origin  https://github.com/repo-B/ (fetch)
origin  https://github.com/repo-B/ (push)
work    file:///tmp/repo-A (fetch)
work    file:///tmp/repo-A (push)

移行後のレポジトリ(repo-B)にマージして、pushする

git checkout -b work-branch # ブランチを切って作業
git fetch work # work(リモート)を取り込む
git merge work/master
git push user work-branch # work-branch(リモートブランチ)にpushする
16
14
3

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
16
14