Git push できない(SSH認証で蹴られる)
SSH 鍵が GitHub に登録されていない、または ssh-agent に追加されていないと push が拒否される。
最も多い原因と対処を順に確認する。
公開: 更新:
症状できない · exit 128
git push -u origin main deploy@127.0.0.1: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
解決後できた · exit 0
git push -u origin main To ssh://127.0.0.1:2222/home/git/repo.git * [new branch] main -> main branch 'main' set up to track 'origin/main'.
2026-10-12 に alpine:3 で実際に打って取った出力。検証環境の詳細
要約
git push が Permission denied (publickey). で失敗するケースは、
SSH 認証の準備が整っていない場合に発生する。
最初に確認するのは
「鍵が GitHub に登録済みか」「ssh-agent に鍵が追加されているか」「リモート URL が SSH か」の3点である。
実行例
alpine:3 のコンテナに sshd と bare リポジトリを立て、リモート URL のユーザーを deploy にして push すると Permission denied (publickey). で止まり、終了コードは 128 だった。
URL を git@ に直しても、公開鍵をサーバー側の authorized_keys に登録するまでは同じ拒否のままで、ssh -T でも同様に弾かれる。
登録後は ssh -T が通り、同じ git push -u origin main が成功している。
$ git remote -v
origin ssh://deploy@127.0.0.1:2222/home/git/repo.git (fetch)
origin ssh://deploy@127.0.0.1:2222/home/git/repo.git (push)$ git push -u origin main
deploy@127.0.0.1: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
$ echo $?
128$ git remote set-url origin ssh://git@127.0.0.1:2222/home/git/repo.git$ git remote -v
origin ssh://git@127.0.0.1:2222/home/git/repo.git (fetch)
origin ssh://git@127.0.0.1:2222/home/git/repo.git (push)$ git push -u origin main
git@127.0.0.1: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
$ echo $?
128$ ssh -T -p 2222 git@127.0.0.1 whoami
git@127.0.0.1: Permission denied (publickey).
$ echo $?
255$ ssh -T -p 2222 git@127.0.0.1 whoami
git
$ echo $?
0$ git push -u origin main
To ssh://127.0.0.1:2222/home/git/repo.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.
$ echo $?
0— 2026-10-12 時点の出力
検証環境2026-10-12 検証
- 検証日
- 実行環境
alpine:3Alpine Linux v3.24
この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。
解決策
1. 鍵の作成と登録
ssh-keygen -t ed25519 -C "you@example.com"
cat ~/.ssh/id_ed25519.pub # この内容を GitHub Settings → SSH and GPG keys に登録2. ssh-agent への追加
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed255193. リモート URL を SSH に切り替え
git remote set-url origin git@github.com:USER/REPO.git4. 疎通確認
ssh -T git@github.com
# Hi USER! You've successfully authenticated...よくある原因
- 公開鍵を登録していない。
GitHub に公開鍵がアップロードされていない。 - ssh-agent に鍵が無い。
端末を再起動したあとにssh-addし忘れている。 - リモートが HTTPS のままで、PAT が期限切れになっている。
古い HTTPS URL に対して PAT が無効になっている。 ~/.ssh/configの設定を誤っている。
別 Host 用の設定が混在し、github.com に到達できない。