LoginSignup
8
3

More than 3 years have passed since last update.

【github】remote: Invalid username or password.の解決方法

Last updated at Posted at 2021-04-19

commitしようとした時にエラーになったのでメモとして残しておきます。

エラー発生までの流れ

①下記画像のようにリポジトリを新しく作成

スクリーンショット 2021-04-19 15.09.13.png

②作成できると下記画像のような画面になる

スクリーンショット 2021-04-19 15.37.55.png

先に伝えておくと、ここで赤枠の部分を見ていただきたいのですがHTTPSを選択しています。
これがそもそもの間違いで、今回の場合はSSHを選択するべきだったのです。
SSHを押下して選択すると下記画像のようになります。

スクリーンショット 2021-04-19 15.32.26.png

何が変わったかというと認証方式が違うのです。
リモートリポジトリの名前の部分も変わっています。

HTTPS->https://github.com/×××/test.git

SSH->git@github.com:×××/test.git

詳細は後述しますので一旦流れを進めます。

③最初のcommitをしようと試みた

git init

git add .

git commit -m "first commit"

git branch -M main

git remote add origin https://github.com/×××/test.git

git push -u origin main

④エラー発生

remote: Invalid username or password.

上記のようにユーザー名もしくはパスワードが違うとメッセージが出ました。
そんなはずはないので困りました。

解決までの流れ

①まず下記の記事で記載されている通り設定を確認しました。

GitHub へのアクセスで remote: Invalid username or password と言われたので、git remote set-url でリポジトリを指定した。

git remote -v

origin https://github.com/×××/test.git(fetch)
origin https://github.com/×××/test.git(push)

httpsで設定してはいけなかったことにここで気づきました。(あくまで今回の場合です)

なので、変更を実施しました。

git remote set-url origin git@github.com/×××/test.git

上記のコマンドを実行すると下記のようにSSHでの認証方式に変更されます。

git remote -v

origin git@github.com:×××/test.git(fetch)
origin git@github.com:×××/test.git(push)

この状態で再度git push -u origin mainを実施。
しかしエラー...

②SSH鍵の認証ができているかを確認

下記コマンドで確認しました。

ssh -T git@github.com

そうすると以下のようなメッセージが出ました。

Hi <username!> You’ve successfully authenticated, but GitHub does not provide shell access.

SSH認証はできているようです。

解決方法

SSH認証はできている、設定もSSH認証にした、ということで以下の方法で解決することができました。

該当のディレクトリで下記のコマンドを実行します。

rm -r ./.git

そのあと、再度以下のコマンドでcommitをします。

git init
git add .
git commit -m "first commit"
git branch -M main
git remote add origin git@github.com:×××/test.git
git push -u origin main

できました!

rm -r ./.git
このコマンドは.gitというgitの設定?などのファイルを一旦削除するというコマンドのようなので実行するディレクトリは気をつけなければいけません。
それから、今回はまだcommitしていないからこのコマンドを使えましたが、毎回使ってはいけないそうです。

先輩たちに感謝!

8
3
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
8
3