0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

リモートリポジトリへのプッシュ時にエラーが出た記録

0
Posted at

1.作業のゴール

ローカルリポジトリにコミットしたファイルを、リモートリポジトリ(GitHub)にプッシュすることが、作業のゴールでした。

2.エラーの内容

プッシュするために $ git push -u origin main のコマンド入力を行った際に、次のようなエラーが出ました。

$ git push -u origin main
\033[200~git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

和訳
「公開鍵認証で拒否されました。
 リモートリポジトリを読み取れませんでした。
 正しいアクセス権があり、
 リポジトリが存在することを確認してください。」

3.原因究明

おそらくキー設定に問題があるのだと考え、次の確認を行いました。

(1) ローカルリポジトリの現状確認

git log コマンドで、ローカルリポジトリにコミットできているのかを確認しました。
こちらは問題なくコミットできていました。

$ git log
commit 9125c9140fd7be3e4ea3a979e187e6276e29e7e0 (HEAn)
Author: user <example@users.norhub.com>
Date:   Fri Oct 10 15:23:43 2025 +0900

    [update]remote-testの追記

commit 9804dcc6dbf8a448f3d56506153a8eddf437047f
Author: user <example@users.norhub.com>
Date:   Fri Oct 10 14:52:43 2025 +0900

    [add]最初のコミット

(2) GitHubとの接続確認

ssh コマンドで、GitHubと接続できるか確認しました。
認証設定は正しく完了していました。

$ ssh -T git@github.com
Hi mt-westfall! You've successfully authenticated, but GitHub does not provide shell access.

(3) .sshフォルダに鍵があるかを確認

$ ls -al ~/.ssh
total 19
drwxr-xr-x 1 [user user]   0 Oct  4 10:33 ./
drwxr-xr-x 1 [user user]   0 Oct 10 15:37 ../
-rw-r--r-- 1 [user user] 399 Oct  4 10:22 id_ed25519
-rw-r--r-- 1 [user user]  94 Oct  4 10:22 id_ed25519.pub
-rw-r--r-- 1 [user user] 828 Oct  4 10:33 known_hosts
-rw-r--r-- 1 [user user]  92 Oct  4 10:29 known_hosts.old

ここで、秘密鍵と公開鍵があるので問題はありません。

id_ed25519 → 秘密鍵
id_ed25519.pub → 公開鍵

(4) ssh-agent に鍵を登録

$ ssh-add ~/.ssh/id_ed25519
Could not open a connection to your authentication agent.

ここで、想定出力と違うエラーが出ました。

(5) ローカルリポジトリとリモートリポジトリの紐づけ確認

git remote -v コマンドで、ローカルリポジトリろリモートリポジトリの紐づけができているかを確認しました。
URLの最後が ~ となっており、不完全な状態となっています。

$ git remote -v
origin  git@github.com:[user]/git-remote-reposi~ (fetch)
origin  git@github.com:[user]/git-remote-reposi~ (push)

紐づけが不完全のため、URLの再登録が必要です。

4.対応

URLの紐づけを修正(変更)する

$ git remote set-url origin [URL]

紐づけの変更を行う場合に使用するコマンドです。
こちらを実行することで、エラーが解消されました。

5.結論

今回のエラー原因は、URLの紐づけが不十分だったことにありました。
リモートリポジトリの設定を変更し、紐づけの方へ反映されていなかったことが、エラーにつながったと考えられます。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?