kiyasuの日記

ハッピーうれピーよろしく哀愁

remote: Permission to user/repo.git denied to xxxxx.

現象

λ gitpush --set-upstream origin dev
remote: Permission to mojonobu/ADM-BWF-ixml-Parser.git denied to xxxxx.
fatal: unable to access 'https://github.com/mojonobu/ADM-BWF-ixml-Parser.git/': The requested URL returned error: 403

というエラーが出ました。

git config -l

で見るとuser.nameはxxxxxではなく、このリポジトリを作ったアカウントのものになっている(だから問題ないはず)のだがこれはどういうことだろう。

とりあえず検索するとこれが出てくる。

docs.github.com

This error means the key you are pushing with is attached to another repository as a deploy key, and does not have access to the repository you are trying to push to.

うーん、commitアカウントはmojonobuだが、pushしようとしているkeyがほかのリポジトリにdeploy keyとして紐づいている、ということのようだ。

原因と対策

gitのconfigにあるuser.nameはあくまでコミットのアカウントで、pushなどに使うアカウント(ログインアカウントとでも呼ぶのかな)は別モノだった。おれはそんなことも意識してなかった…

git config --local -l

で出てくる次の行

remote.origin.url=https://github.com/mojonobu/ADM-BWF-ixml-Parser.git

これを 「git config --local -e」コマンドで次のように変える

remote.origin.url=https://(user名)@github.com/mojonobu/ADM-BWF-ixml-Parser.git

するとpushしようとしたときに次のような画面が出る。

f:id:mojo_nobu:20220130205132p:plain

githubPersonal Access Tokenを取得して入力すればプッシュできました。

デフォルトでプッシュしようとするアカウントはどこで設定されているんだろうか?不明である。

ユーザーを指定してgit clone | ハックノート

gitで別のアカウントにpush/pullしたい場合 - Qiita

これはSSHの場合。いつか使うかもしれない。

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