だれも聞いていないと思って歌え

dance as if no one’s watching, sing as if no one’s listening, and live everyday as if it were your last.

git リポジトリの内容を別の git リポジトリに移す

repoA, repoB のように別々に管理し作業をしていたリポジトリを、統合する必要があったので repoB を repoA にまとめることにした。

変更前

.
├── repoA
└── repoB

変更後

.
└── repoA
    └── repoB

のようにしたい。

今回は、諸事情から git submodule で repoB をサブディレクトリとして登録するのではなく、 repoA で全てのソースコードを管理するように変更します。

手順は、下記記事を参考にしています。

qiita.com

# repoB のディレクトリをリモートとして追加
$ git remote add repoB ../repoB
$ git fetch repoB

# 予め、 repoB という名前のディレクトリを作成
$ mkdir repoB

# repoB の master ブランチを、 repoB ディレクトリにマージ
$ git merge -X subtree=repoB repoB/master --allow-unrelated-histories origin/master

最後のマージ時だが、 --allow-unrelated-histories オプションなしの場合だとエラーが表示される。

$ git merge -X subtree=repoB repoB/master
fatal: refusing to merge unrelated histories

どうやら、履歴の全く異なるブランチをマージする際に表示される警告らしく、オプションをつけることで解決しました。

qiita.com

ついでに、 repoB では GitHub Actions で lint を実行していたので下記のようにディレクトリを指定するように変更。

name: lint
on:
  push:
    paths:
      - 'repoB/*'
      - '.github/workflows/lint.yml'

defaults:
  run:
    working-directory: ./repoB

jobs:
  lint:
    runs-on: ubuntu-latest
      ...

lint の実行を、 repoB と、このファイル自身である lint.yml に限定し、コマンドの実行時に対象としたいディレクトリを working-directory で初期設定する。

cd コマンドは書く必要がなく、 repoB ディレクトリ下の package.json と紐づいた内容が無事に実行されるようになりました。

docs.github.com

github.community