LoginSignup
184
162

More than 3 years have passed since last update.

複数のgitアカウントを使用する場合

Last updated at Posted at 2017-12-18

仕事とプライベートとで別々のGitアカウントを使用する必要が出てきた際に、プライベートと仕事での開発とで環境を切り替えがわりと面倒だったので、今後迷わないように記録する。

アカウントの新規作成(ざっくり)

 1. 新しいメールアドレスでGithubアカウントを作る
 2. sshキーを作る
~/.ssh/conifgファイルに下記の様なフォーマットで秘密鍵のパスを指定する。

Host github
  HostName github.com
  IdentityFile ~/.ssh/main_rsa
  User git
  Port 22
  TCPKeepAlive yes
  IdentitiesOnly yes

Host github-sub
  HostName github.com
  IdentityFile ~/.ssh/sub_rsa
  User git
  Port 22
  TCPKeepAlive yes
  IdentitiesOnly yes

# github-subとgithubは任意
# IdentityFileのファイル名も任意

3. 接続確認
ssh -T <HOST>と打つとssh接続ができるか確認できる。
接続が完了すると下記の様なレスポンスが返ってくる。

Hi takuyama29! You've successfully authenticated, but GitHub does not provide shell access.

(エラーっぽいけどエラーじゃないから大丈夫)

4. remote url設定

git remote add origin git@github.com:<ACCOUNT_NAME>/<YOUR_REPSITORY>.git

5. ターミナルのアカウント切り替え

$ git config --global user.name "<YOUR_NAME>"
$ git config --global user.email <YUOR_EMAIL>

これで新しいアカウントの追加が出来てめでたしめでたし。

困ったこと

困ったのはここから。
このまま元のGitアカウントに戻って作業してpushとかするとエラーになる。

ERROR: Repository not found.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

"リポジトリはあるけど、アクセス権が無いよ"

と言われて困る。
この事象の原因は2つある。

1. Githubユーザーが違う

ターミナルで設定されているGithubアカウントが意図しているものと異なっている。

現在設定されているアカウント情報は$ git config --listで一覧表示することが可能。
その中のuser.nameuser.emailが新しいアカウントのままになっていることが今回の事象の原因の一つ。

そのため、本来は開発作業をするアカウント別にCLIの設定も変更する必要がある(↓コレ)。

$ git config --global user.name "<YOUR_NAME>"
$ git config --global user.email <YUOR_EMAIL>

これを毎回切り替えるのは面倒なので、せめてコマンドで切り替えたい。

大した知識もないため、一旦shellコマンドとして利用するためにrcファイルに下記を追記した。

function tomain() {
  git config --global user.name "<MAIN_ACCOUNT>"
  git config --global user.email <MAIN_ADDRESS>
}

function tosub() {
  git config --global user.name "<SUB_ACCOUNT>"
  git config --global user.email <SUB_ADDRESS>
}

これで$ tomain$ tosubでひとまずアカウントの切り替えができる。
※もっとちゃんとしたやり方があると思うので、知っている方はご教示いただければと!

2. remote urlのHost名が重複している

これも微妙に気づくのに時間がかかった。
冒頭で書いた~/.ssh/configファイルの追記内容。

Host github
  HostName github.com
  IdentityFile ~/.ssh/main_rsa
  User git
  Port 22
  TCPKeepAlive yes
  IdentitiesOnly yes

Host github-sub
  HostName github.com
  IdentityFile ~/.ssh/sub_rsa
  User git
  Port 22
  TCPKeepAlive yes
  IdentitiesOnly yes

これだと微妙に困ったことになる。

$ git remote add origin git@github.com:<ACCOUNT_NAME>/<YOUR_REPSITORY>.git

した時に、HostNameがどちらもgithub.comなので、configファイルの下部に定義されたものが有効になってしまう。

この場合だと、Githubへ接続する際には、毎回IdentityFile ~/.ssh/sub_rsaを参照してしまうので、メインアカウントでの接続の際に秘密鍵と公開鍵の整合性が合わなくなるからだ。

そのため、remote urlを設定する際には、Host名が重複しない様に設定する必要がある。

メイン

$ git remote add origin git@github:<ACCOUNT_NAME>/<YOUR_REPSITORY>.git

サブ

$ git remote add origin git@github-sub:<ACCOUNT_NAME>/<YOUR_REPSITORY>.git

上記の様に~/.ssh/configファイルで設定したHost名別に分けてremote urlを設定すればOK

これ系のちょっとした環境設定とかは頻度も少ないので、少しずつ記録として残していきたい。

184
162
1

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
184
162