事象
git push を実行してローカルの変更を GitHub 上のリモートリポジトリへ push しようとしたところ、以下のようなエラーが発生しました。
$ git push
Username for 'https://github.com': john.smith@example.com
Password for 'https://john.smith@example.com@github.com':
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/john.smith/hello-world.git/'
原因
2021年8月13日以降、GitHub ではセキュリティ強化の一環として、HTTPS 経由での Git 操作において、GitHub アカウントのパスワードによる認証が廃止されました。これにより、従来のようにパスワードを入力して push することができなくなっています。
解決方法
現在推奨されている認証方法はいくつかありますが、私が実際に利用したことのある方法を3つ紹介します。
1. GitHub CLIを利用する
GitHub CLIを使えば、ターミナル上で簡単に認証を行うことができます。
- GitHub CLIをインストールする
- 認証を作業を行う
gh auth login
2. GitHub Desktopを利用する
ターミナル操作にこだわりがなければ、GitHub Desktopを使うのも簡単です。GUIで直感的に操作できます。インストール後に GitHub アカウントでログインすれば、push や pull などの操作もシームレスに行えます。
3. PATを利用する
パスワードの代わりに、Personal Access Token(PAT)を使って認証する方法です。セキュリティ上、トークンの管理に注意が必要ですが、最も直接的な方法でもあります。
- GitHubの開発者設定からPersonal Access Tokenを発行する
- git push時にパスワードの代わりに発行したトークンを入力する
環境
- git version 2.43.0.windows.1