はじめに
私用のgitHubアカウントのbranchを2週間ぶりに触ったら、なぜかgit pushだけできなくなっていたので、その事象と解決方法を記録しておきます。
事象
git pushすると、アクセス権限系のエラーが返ってくる
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
でも、git pull, git fetch, git commit
などpush以外の基本的なコマンドは動いてました。ネットで調べてみると、public Keyを登録する解決方法が多かったですが...もう設定してあるはずなんですよね...
以下問題を調べるために確認したこと、コマンド一覧です。
確認したこと
- ssh public keyがgitHubに登録されているか? => 登録済
- SSH設定ファイル(~/.ssh/config)の設定は? => 設定済
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
- リモートレポジトリとブランチは存在するか? => 存在する
git ls-remote --heads origin
491ce0c4c760c02fc123be5c57802ba0056de7ca refs/heads/develop/9_palindrome-number
68625d48dedb9fa95b36f5c63d5ab6f841f0cba1 refs/heads/main
- リモートレポジトリへの書き込み権限はあるか? => 書き込み権限ある
ssh -T git@github.com
Hi araiWorks! You've successfully authenticated, but GitHub does not provide shell access.
- ローカルブランチとリモートブランチの追跡設定はちゃんとできている? => OK
git branch -vv
develop/9_palindrome-number 491ce0c solved with strrev
* main 491ce0c [origin/main: ahead 1] solved with strrev
- リモートブランチの状態を確認 => OK
git fetch origin #最新を引っ張ってくる
git status #リモートブランチとローカルブランチが一致しているかみる
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
解決方法
public key周りはもう登録してあるので、とりあえず一回gitのディレクトリを消して、もう一度git cloneしてみようと思いました。普通にgit cloneしただけだとまた同じ現象になったので、以下のサイトを参考にさせていただきcloneしました。
1. SSH設定を確認
~/.ssh/config
Host github
HostName github.com
IdentityFile ~/.ssh/id_git_rsa
User git
2. SSH接続確認
ssh -T git@github
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
3.Hostを指定してgit cloneする
git clone git@github:username/repository-name.git
これでgit pushも特にエラーなくできるようになりました!
最後に
直ってよかった...
参考になりましたら、幸いです🍎