背景
本来同じレポで管理したかったものが2つのレポになってしまっていたので、一つのレポにしておきたいなと思って調べた。
今回は、repo_srcとrepo_dstがあって、repo_srcの target_branch
ブランチをそのままrepo_dstの target_branch
ブランチに移動する
ステップ
1. 変数をセット
後で同じコマンドを使えるように、こんな感じでセットしておく
REPO_SRC=https://github.com/nakamasato/repo_src
REPO_DST=https://github.com/nakamasato/repo_dst
REPO_SRC_PATH=~/repos/repo_src
REPO_DST_PATH=~/repos/repo_dst
TARGET_BRANCH=target_branch
2. repo_srcとrepo_dstをローカルにClone
GitのRemoteにPushしてない場合はCloneは不要。単純にREPO_SRC_PATHとREPO_DST_PATHをセットすればOK
git clone $REPO_SRC $REPO_SRC_PATH
git clone $REPO_SRC_PATH $REPO_DST_PATH
3. repo_dst側でremote repo_src_remote
をセットする
cd $REPO_DST_PATH
git remote add repo_src_remote $REPO_SRC_PATH
4. repo_dst側でTARGET_BRANCH
を作成しておく
git branch -b $TARGET_BRANCH
5. repo_dst側でTARGET_BRANCH
をrepo_srcのTARGET_BRANCH
にresetする
git reset repo_src_remote/$TARGET_BRANCH --hard
これでrepo_dst
レポ側の$TARGET_BRANCH
にrepo_src
の$TARGET_BRANCH
のコミットがすべて移動できた。もちろんターゲットブランチがrepo_dstとrepo_srcで異なってもできる。
6. 不要になったremoteをrepo_dstから削除する
git remote rm repo_src_remote