LoginSignup
0
0

More than 1 year has passed since last update.

あるGit branchを別のGit repoに移動する

Last updated at Posted at 2022-08-18

背景

本来同じレポで管理したかったものが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_BRANCHrepo_src$TARGET_BRANCHのコミットがすべて移動できた。もちろんターゲットブランチがrepo_dstとrepo_srcで異なってもできる。

6. 不要になったremoteをrepo_dstから削除する

git remote rm repo_src_remote

参考

0
0
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
0
0