ssh接続されたgitアカウントに対して別アカウントからpushできるようにする

概要

ssh接続されたgitアカウントに対して別アカウントからpushできるようにする。 git push origin mainした時に Permissionエラーになった。

ssh接続で制限されている別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:TOMOYUKI-n/ManagementTwitter.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