LoginSignup
7
3

More than 3 years have passed since last update.

個人用と会社用のgithubのアカウントを統合した話

Last updated at Posted at 2020-04-15

はじめに

アカウント統合など何度も行う作業ではない。忘れたことにやってくるので書き置きしておく。

githubのアカウントを会社用と個人用に分けて使っていた。
会社用をメイン、個人用をサブという位置付けで、sshキーを2種類用意して

~/.ssh/config
Host github.com # メインアカウント(会社用)
  HostName github.com
  User git
  Port 22
  IdentityFile ~/.ssh/id_rsa
  TCPKeepAlive yes
  IdentitiesOnly yes

Host github.com.sub # サブアカウント(個人用)
  HostName github.com
  User git
  Port 22
  IdentityFile ~/.ssh/id_rsa_sub
  TCPKeepAlive yes
  IdentitiesOnly yes

基本的に会社用は意識せずに使って、サブのときだけ切り替えるという感じで運用していた。

しかし、GitHub利用規約には

1 人の個人または 1 つの法人が複数の無料「アカウント」を保持することはできません (コンピュータアカウントも制御することを選択した場合、それは問題ありませんが、それはコンピュータの実行にのみ使用できます)。

と書いてあったりする。
何よりも切り替えが無い方が(精神的に)楽である。

やりたいことは、会社アカウントに紐づいているorganizationやリポジトリを個人アカウントに紐付けし直して、個人アカウントのみで完結する、ということだ。

切り替え作業

ココは会社にお願いしましょう。
その間に個別のリポジトリがあれば、個人アカウントにtransferしておきましょう。

切り替え作業後

全て紐付けし直された!さてローカルのリポジトリを確認してみよーとなると、

ERROR: Repository not found.
fatal: Could not read from remote repository.

となって、分かってはいるけど少し焦る。
.ssh/configに書いてあれば、あーそうか、と思い出すが、configに書いてない場合、デフォルトのid_rsaを読みに行ってしまうので要注意。

$ ssh -T git@github.com
Hi xxxx! You've successfully authenticated, but GitHub does not provide shell access.

確認すると会社アカウントの名前が表示される。

先ほどのconfigを修正。

~/.ssh/config
Host github.com # メインアカウント
  HostName github.com
  User git
  Port 22
  IdentityFile ~/.ssh/id_rsa_sub ←ココを書き換え
  TCPKeepAlive yes
  IdentitiesOnly yes

サブのHostを削除して、サブで使っていたIdentityFileをメインのところに上書きすればOK!(これはサブを残してメイン削除でもよい)

$ ssh -T git@github.com
Hi oooo! You've successfully authenticated, but GitHub does not provide shell access.

個人アカウント用のSSHキーに切り替わった。

あとは、

remote.origin.url=git@github.com.sub:[organization]/[repository].git
↓
remote.origin.url=git@github.com:[organization]/[repository].git

.subが付いているところを削除すればOK。

もう一度ローカルのリポジトリを確認すると

ERROR: The xxx organization has enabled or enforced SAML SSO. To access this repository,
you must use the HTTPS remote with a personal access token or SSH with an SSH key and passphrase...

と表示される場合がある。
そうしたら、githubのSettingsから
capture 2020-04-15 14.51.24.png
左メニューのSSH and GPG keysを選択し、Enable SSOまたはDisable SSOを押すと
capture 2020-04-15 14.51.41.png
と表示されるのでAuthorizeを押す。

以上でエラーも消えて、何事もなかったかのように以前のままローカルリポジトリを使うことができた。

最後に

あとはgit configでnameとemailを適当に設定してください。

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