やりたいこと
githubのマルチアカウント環境構築を構築する
e.g. 基本的には普段使うアカウントAを使うが特定ディレクトリ配下では、アカウントBを使う
困ったこと
git config --local
でuser.nameとuser.mailを設定するだけで良いかと思ったら、remote repository not found
で怒られ続けた。特にsubmoduleでハマった。
やっったこと
- rsa鍵を追加
- configファイル作成
- 使用するURLを変更した
前提
OS: macOS Sierra 10.12.5
アカウントBとして操作したいディレクトリ配下で、git config --local ..
で、user.nameとuser.mailを設定済み
詳細手順
rsa鍵を追加
1 - アカウントBのメアドでRSAキーを作る
$ ssh-keygen -t rsa -C "foo@bar.com"
多分id_rsaは既存のものがあるので、別ファイルを作りましょう
ここではid_rsa_hoge
とします。
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user_name/.ssh/id_rsa):
2 - sshキーを追加する
ssh-add ~/.ssh/id_rsa_hoge
3 - 追加されていることを確認します
$ ssh-add -l
configファイル作成
1 - $ vim ~/.ssh/config
Host acountB.github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_hoge
2 - テストする
ssh -T git@acountB.github.com
アカウントBのユーザー名で下記のように認証が通ればオッケー。
Hi {your_user_name}! You've successfully authenticated
, but GitHub does not provide shell access.
使用するURLを変更した
1 - アクセスしたいURLを確認する
こんなのだったとしましょう
https://github.com/user/Repo.git
2 - URLを修正する
クローンしたいとして
git clone git@acountB.github.com:user/Repo.git
でOK
2' - submoduleをなんとかしたい場合
親レポジトリをクローンしたディレクトリにて、.gitmodulesを編集する。
vim .gitmodules
[submodule "sub_repo"]
path = sub_repo
url = https://github.com/user/sub_repo.git
↑のような設定だったら、
url = git@acountB.github.com:user/sub_repo.git
と書き換える
そして
$ git submodule sync
これにより通常の操作が可能となります。
参照リンク
- https://gist.github.com/jexchan/2351996
- https://stackoverflow.com/questions/3336995/git-will-not-init-sync-update-new-submodules
githubとstackoverflowに感謝です