事象

別PCで、グローバルにいつもと違うユーザを使用しており、 プライベートのリポジトリにpushしようとしたらエラーが発生する。

$ git push -u origin master
ERROR: Permission to user1/hogehoge.git denied to user2.
fatal: Could not read from remote repository.

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

対応

sshのconfigに追記

# 元々記載
Host github.com
  HostName github.com
  IdentityFile ~/.ssh/id_rsa
  User git
  TCPKeepAlive yes
  IdentitiesOnly yes
.
.
.
# 追記する
Host mygithub.com
  HostName github.com
  IdentityFile ~/.ssh/my/id_my_git_rsa
  User git

remote情報を書き換え

$ git remote set-url origin git@mygithub.com:user1/hogehoge.git

変更後

$ git remote -v
origin	git@mygithub.com:user1/hogehoge.git (fetch)
origin	git@mygithub.com:user1/hogehoge.git (push)

変更前

$ git remote -v
origin	git@github.com:user1/hogehoge.git (fetch)
origin	git@github.com:user1/hogehoge.git (push)

再実行

$ git push -u origin master
Counting objects: 10, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (9/9), done.
Writing objects: 100% (10/10), 1.89 KiB | 965.00 KiB/s, done.
Total 10 (delta 0), reused 0 (delta 0)
To mygithub.com:user1/hogehoge.git
   63995c5..ce30979  master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

want

Hostではなく、userで切り分ける方法はないかな。