gitgithubsshremote-repository

How to resolve remote SSH push github error


I have created SSH key linked to github. I am trying to remotely push a file to the created repository

these are my code

git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/username/demo_repo2.git
git push -u origin main

But it is giving me error as :

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.

I have checked whether ssh key is linked , and it is. It is even giving

git remote -v

origin  git@github.com:user/repo2.git (fetch)
origin  git@github.com:user/repo2.git (push)

I tried almost all answers , but not working . Please someone help me


Solution

  • It should not give you this error, since you have set an HTTPS URL with:

    git remote add origin https://github.com/username/demo_repo2.git
    

    Check git config -l for any url."git@github.com:".insteadOf https://github.com/ directive.

    If git remote -v does give you an SSH URL, that means the first git remote add origin must have failed.
    You can force an HTTPS URL with:

    cd /path/to/my/repo
    git remote set-url  origin https://github.com/username/demo_repo2.git