Permission Denied (publickey)

I am trying to push my locally created gitrepo to gitlab first time at that time I execute this command for pushing files.

git push --set-upstream git@gitlab.com:user_name/work.git master

and I am facing below error:

git@gitlab.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

Hi @abhishek1110

Sorry you’re having issues pushing to GitLab. There are a few things that might be coming into play. Can you please check that:

Take any steps not yet taken and let us know if the problem persists.
I believe this process should fix the issues you’re having.

2 Likes

Hi @gitlab-greg

Thanks for replying,

I am creating Project from scratch on local and then trying to push on gitlab.com so I wanted to create project on gitlab.com using gitbash.

I am working in work directory
D:/Atoll Stuff/HWREPO-2019-08-06/work/

and this is what I am getting

$ git init
Initialized empty Git repository in D:/Atoll Stuff/HWREPO-2019-08-06/work/.git/

adding my project files in work directory

$ git add -A

$ git remote add origin git@gitlab.com:user_name/work.git

$ git commit -m “first commit”
[master (root-commit) a42d0dc] first commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 New Text Document.txt

$ git push --set-upstream git@gitlab.com:user_name/work.git master
git@gitlab.com: Permission denied (publickey).
fatal: Could not read from remote repository.

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

Hi @abhishek1110,

Thanks for the response and details.

The issue here is that you must create the project on GitLab before you can push to it as a remote origin. Without having a project created on GitLab.com, there is no “remote origin” available to push to.

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

This error is basically Git saying “I tried to push to remote, but the remote origin does not exist”.

The solution is to create a blank project on your GitLab.com named work and then push to it. You can create a new project in the web interface, or you can use the Create Project API endpoint. Either way, if the project exists on GitLab.com before you push to it, you can get past this error.

Hi @gitlab-greg

Thanks for the solution.

Hi @gitlab-greg,

I am confused. You say above, one must create a project first using the portal or API, however, in your article here, you say one can run git push --set-upstream... to create a new project.

When you create a new repository locally, instead of going to GitLab to manually create a new project and then clone the repo locally, you can directly push it to GitLab to create the new project, all without leaving your terminal. If you have access rights to the associated namespace, GitLab will automatically create a new project under that GitLab namespace with its visibility set to Private by default (you can later change it in the project’s settings).
This can be done by using either SSH or HTTPS:

Does the referenced article relate to some other “version” or something?

TIA

Hi @woter324,

Thank you very much for pointing this out and asking an excellent question.

I honestly was not aware that this functionality exists, but I can confirm that following the steps here is a valid way to create a new project without having to use the UI or API:
https://docs.gitlab.com/ee/gitlab-basics/create-project.html#push-to-create-a-new-project

Knowing this, I believe the original poster’s (@abhishek1110) problem was actually related the result of not having the correct SSH public key added to the user’s GitLab.com account as indicated by Permission denied (publickey)

When seeing Permission denied (publickey) in errors that arise with git commands and GitLab.com, here are two options to help troubleshoot and identify the problem:

as Matt Hagemann said

All you do is move the SSH keys we’ve generated above to the root’s SSH directory located at /root/.ssh. If you try sudo git clone now

1 Like

Hi,

I have been experiencing the same issue with this type of setup. The public key is denied due to some permissions issue. I have tried the methods on the this post, but still experiencing issues. Any help would be greatly appreciated.

@Cybersecurelabs2020 If the public key is denied because of permissions issues, its likely that the permissions need to be changed on your local system.

https://help.ubuntu.com/community/SSH/OpenSSH/Keys#Permission_denied_.28publickey.29

In my case I needed to switch from git remote add origin git@gitlab.com:user/the-repo.git to git remote add origin https://gitlab.com/user/the-repo and that worked for me, as the empty repository was already created in Gitlab.

The first try (which is the one the documentation shows) didn’t work for me even after changing my password in the git config --list since it was also wrong.