概要
gitアカウントに対して別アカウントからpushできるようにしたら便利だな〜と思い、
git push origin main
した時に Permissionエラーになった。
どうやらssh接続でcloneは誰でもできるがpushは特定のユーザーしかできないように過去の自分は設定していたようだ(偉い。。) 別gitアカウントのレポジトリにpushできるように調べた。
エラー内容
ERROR: Permission to [ユーザーID]/[レポジトリ名].git denied to [会社のgitのユーザーID]. fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
対応
- 別アカウントhogeからアクセスできるように鍵を作成
$ ssh-keygen -t rsa -f id_rsa_hoge -b 4096 -C "" // 何も入力せずEnter or パスワードとなる文字列を入力
$ cd ~/.ssh/ $ vi config Host github_hoge <---- アクセスするときの名称 HostName github.com IdentityFile ~/.ssh/id_rsa_hoge <----自分の秘密鍵 User git
- 公開鍵の中身をコピー(mac)
$ pbcopy < ~/.ssh/id_rsa_hoge.pub
アクセスしたいレポジトリのユーザーの「setting」 → 「SSH and GPG keys」で公開鍵を追加 ※レポジトリのsettingではない!
テスト
$ ssh -T github_hoge $ Hi hoge! You've successfully authenticated, but GitHub does not provide shell access.
- リモートレポジトリでmainがあれば削除しておく
- ローカルにクローン
$ git clone git@github.com:hoge/sample_repository.git
- リモートの向き先でホスト名を変更。(~/.ssh/configで設定している場合)
$ cd レポジトリのディレクトリ $ git remote set-url origin [Host名]:[ユーザID]/[リポジトリ].git // こんな感じになる [remote "origin"] url = github_hoge:hoge/sample_repository.git
- push
$ git push origin main
参考になりました、ありがとうございます。
https://tofusystem.work/programming-hack/easy-ssh-login-setting/