LoginSignup
1
1

More than 3 years have passed since last update.

一つのローカルリポジトリに、複数アカウント & それぞれのアカウントに紐づくリモートリポジトリを設定する方法

Last updated at Posted at 2020-01-09

一つのローカルリポジトリから、複数のアカウントかつそれぞれリモートリポジトリにpushやcloneする方法をご紹介します。

ssh接続

使用したいリポジトリでssh接続を行っておく。

該当ディレクトリに移動

$cd ~/.ssh

サブアカウント用の鍵を作成

メインアカウントでは既に設定してあると思うので、今回追加したいアカウント用に作成する。

参考:GitHubでssh接続する手順~公開鍵・秘密鍵の生成から~

$ssh-keygen -t rsa

と打つと

Enter file in which to save the key (/Users/(username)/.ssh/id_rsa):

と聞かれるので、任意のファイル名を入力する。今回はsub_rsaとしておく。

以下2つの質問はEnter, Enterで問題ない。

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

公開鍵のアップ方法、接続確認方法はこちらを参照お願いします。

参考:GitHubでssh接続する手順~公開鍵・秘密鍵の生成から~

configファイルの作成

Host sub以下を追加

  • main, subには任意の名前
  • IdentityFileには先程作成した鍵のファイル名を記載
~.ssh/config
Host main
  HostName ssh.github.com
  IdentityFile ~/.ssh/id_rsa
  User git
  Port 22

Host sub
  HostName ssh.github.com
  IdentityFile ~/.ssh/sub_rsa
  User git
  Port 22

メインアカウント・メインのリモートリポジトリを追加

  • @の後にmainを入れる
git clone git@main:main_account/main_repository.git
git remot -v

origin  git@main:main_account/main_repository.git (fetch)
origin  git@main:main_account/main_repository.git (push)

サブアカウント・サブのリモートリポジトリを追加

  • [ラベル名]にはpushやpullする時に指定できるようラベルを入力する
  • @の後にsubを入れる
git remote add [ラベル名] git@sub:sub_account/sub_repository.git

確認

git remot -v

[ラベル名]  git@sub:sub_account/sub_repository.git (fetch)
[ラベル名]  git@sub:sub_account/sub_repository.git (push)
origin  git@main:main_account/main_repository.git (fetch)
origin  git@main:main_account/main_repository.git (push)

メインリポジトリへのpush

git push origin master

サブリポジトリへのpush

git push [ラベル名] master

Githubアカウントを複数もつということがGithubの思想とは異なっているかもしれませんが、
使用機会があると思いましたので、共有させて頂きました。

参考

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