9
16

More than 3 years have passed since last update.

git pushで403エラーが返された話

Last updated at Posted at 2020-01-13

はじめに

  • Gitの勉強をしていて、push時に403エラーが返された

エラー内容

$ git push origin master
remote: Permission to アカウント1/リポジトリ名.git denied to アカウント2.
fatal: unable to access 'https://アカウント1@github.com/アカウント1/リポジトリ名.git' : The requested URL returned error: 403
  • アカウント1が、アカウント2にアクセスしようとしていた
  • 当たり前だけどアカウント1にはアカウント2にアクセスする権限がないので、アカウント2にアクセスが拒否されて403エラーが返されたっぽい

解決方法

  • アカウント1にアクセスするようにリモートURLを以下のように変更する
$ git remote set-url origin https://アカウント1@github.com/アカウント1/リポジトリ名.git
  • 以下のコマンドを実行して、リモートURLが変更されていることを確認する
$ git remote -v
origin: https://アカウント1@github.com/アカウント1/リポジトリ名.git (fetch)
origin: https://アカウント1@github.com/アカウント1/リポジトリ名.git (push)
  • 再度git push origin masterを実行しエラーが出ないことを確認する
$ git push origin master
  ~一部省略~
To https://github.com/アカウント1/リポジトリ名.git
 * [new branch]      master -> master

成功!

解決するまでにやったこと

  1. エラー内容の解析
  2. 現在のリモートURLの確認
  3. 1、2からエラーを解決する方法を検索

2.で使用したコマンド

$ git remote -v
origin: github.com/アカウント/リポジトリ名.git (fetch)
origin: github.com/アカウント/リポジトリ名.git (push)

3.で参考にさせていただいた記事
GitHub ヘルプ:リモートの URL の変更
https時代のgitアカウントを使い分ける方法@1natsu172さん

まとめ

  • 今回のエラーは、現在使用しているアカウントが別のアカウントにアクセスしようとしたため発生したものだと分かった
  • 疑問なのは、なぜ別のアカウントにアクセスしようとしたのか。
    • 追記 1/16)上記疑問について調べた結果、「どのアカウントでアクセスするか指定していない状態だったため、アカウントの切り替えが出来ていなかった」ということがわかった。そのため、上記作業でアカウントを指定してあげたということになる
9
16
0

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
9
16