GitHubで2FAを有効にした時にgit pushでAuthentication failedや403エラーになった時の対応

GitHubで途中でアカウントの2FAを有効にした場合、今まで使っていたリポジトリgit push origin xxx を実行すると、

remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/hoge/some-repo.git/'

とか、

fatal: unable to access 'https://github.com/hoge/some-repo.git/': The requested URL returned error: 403

といったエラーに遭遇します。
その時の対応法です。

解決法

  1. 新しくPersonal access tokensを発行する
  2. .git/configに記載されている urlhttps://{account name}@github.com/hoge/some-repo.git の形式にする
  3. 初回push時にパスワードを聞かれるので、1.で発行したPersonal access tokenを入力(ペースト)する

参考