LoginSignup
8
3

More than 3 years have passed since last update.

githubで複数アカウントをgitで利用するときにハマッたときの解決方法

Last updated at Posted at 2019-06-12

githubで個人、会社など複数アカウントを利用していると下記のような ~/.ssh/config を書いて切り替えて使うことがあると思われる。

~/.ssh/config
Host github1
  HostName github.com
  IdentityFile ~/.ssh/id_hogehoge1_for_enterpirse
  User git
  TCPKeepAlive yes
  IdentitiesOnly yes
Host github2
  HostName github.com
  IdentityFile ~/.ssh/id_hogehoge2_for_personal
  User git
  TCPKeepAlive yes
  IdentitiesOnly yes
  • 切り替えるとき
# github1 を使うとき
$ssh github1

# github2 を使うとき
$ssh github2

これで、例えば、github1のアカウントのプライベートリポジトリを git clone するときに、下記のようにやるとエラーになる。

$ git clone git@github.com:hogehoge1/private_repo.git
Cloning into 'private_repo'...
ERROR: Repository not found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

ここの git@github.com:hogehoge1/private_repo.git は、githubのclone or download ボタン からコピーしたものだがエラーになってしまう。

この場合は、下記のようにするとgit cloneできる。


$ git clone git@github1:hogehoge1/private_repo.git

git@github1:hogehoge1/private_repo.gitgithub1 部分は ~/.ssh/configHostを指定する必要がある。
githubで複数アカウントを利用していないと気づかない落とし穴だと思いました。結構ハマってしまった。。。

8
3
2

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