Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

git@gitlab.com: Permission denied (publickey). fatal: Could not read from remote repository

Ask Question

I am using macOS Catalina. I already have a repository on GitLab and an SSH-key assigned. Now I want to create another repository from the terminal. I do the following:

git config user.name my_name
git config user.email my_email
git init

Then I get this:

Initialized empty Git repository in directory

So far so good.

git remote add origin git@gitlab.com:my_name/repo.git
git add .
git commit -m 'commit'
git push -u origin master

Then I get the following 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.

Then I go to the repository I already had and try to push there, everything works so I guess I don't have a problem with SSH-key. I know this is a very common question on the internet but none of the answers solved my problem.

It's probably because the repository does not exist on the remote server. I'm sure there's some fancy way to achieve that from the command line, but I've always found it easier to create the repo on github/gitlab and clone that to my local drive. – CoryCoolguy Jan 8, 2020 at 21:40 @CoryCoolguy I tried creating the repo in gitlab and then cloning into my local computer however I got the same error again... :( – beliz Jan 8, 2020 at 21:49 Have you assigned your ssh key into your gitlab account? After login, navigate to https://gitlab.com/profile and on the toolbar choose ssh-keys. Paste your public key in the textbox that shows up and add your key. – Naor Levi Jan 8, 2020 at 22:01 I already assigned my ssh key to gitlab and it is working in the other repository... @NaorLevi – beliz Jan 8, 2020 at 22:04 Make sure to do git init before git config commands, or those would fail, since you are not in a Git repository. – VonC Jan 8, 2020 at 22:13

First, you should get "Initialized empty Git repository in directory" only after a git init ., not after a git remote add origin ...

Second, with GitLab, you can push to create a new project, as illustrated in this MR, starting with GitLab 10.5 (Q1 2018)

Third, if the error persists, then the key is somehow at fault.
Test it with:

ssh -Tv git@gitlab.com
git -c core.sshCommand="ssh -v" push -u origin master

To generate a valid key:

ssh-keygen -t rsa -P "" -m PEM

And register your new id_rsa.pub to your GitLab profile.

When I do ssh -Tv git@gitlab.com, it prints a lot of lines and gives git@gitlab.com: Permission denied (publickey). and I see that it is looking for the ssh-key in .id_rsa file. However when I assigned the key to my gitlab profile I used the id_ed25519. Then I did ssh -Tv git@gitlab.com in the other repository where it I can commit I also see that it is looking for .id_rsa and then I get git@gitlab.com: Permission denied (publickey). again but I can push. I don't understand... – beliz Jan 8, 2020 at 22:13 @beliz The other repo wouldn't have an HTTPS URL by any chance? (check the git remote -v output) – VonC Jan 8, 2020 at 22:14 I used id_ed because I already have an rsa key associated with work email for another gitlab account. I didn't wanted to mix them up. – beliz Jan 8, 2020 at 22:18 @beliz You can leave the HTTPS URL in the first repo. Once SSH will be working, you would be able to modify your origin URL of that first repo with git remote set-url origin git@gitlab.com:<name>/<repo>. – VonC Jan 8, 2020 at 22:23

I tried all the above mentioned solutions but none of it worked. I then read the logs and found that it is looking for the key in a specific folder and I created the key and added it to my Gitlab profile too. Then it started working.

Git authentication issue can be solved by reading the logs of the git and creating appropriate SSH keys under appropriate folders.

Steps
  • Run the following command and it will try to push the code and if it not successful then it will display where the error is

    git -c core.sshCommand="ssh -v" push -u origin master
    
  • Now, we can generate a new SSH key and the following command will generate a key in the working folder.

    ssh-keygen -t rsa -P "" -m PEM
    

    It will ask for key name, you can give id_rsa as the key name or any name which the Bash displays as "Trying private key: c:/Users/Dell/.ssh/". Once the key is generated in bash, your working directory will have the key.

  • While running the command in step1, you will see that the folder in which it is looking for a private key. In my case it is "c:/Users/Dell/.ssh/id_rsa"
  • We should put the generated keys from the working folder into this folder
  • We should also make sure that we add our SSH Key to the Gitlab account. Click on your Gitlab account MyProfile and select preferences. Click to see how to add SSH to your Gitlab account

  • Click the SSH keys menu, open the generated key file using notepad and copy the content of the key from notepad and paste it in the SSH key text editor and save it . Click to see how to add SSH Key to your Gitlab account

  • Again, run the following command and check now. The code will be pushed.

    git -c core.sshCommand="ssh -v" push -u origin master
    

    The same issue happened. I used HTTPS instead of SSH

    (I followed the instruction steps after creating repo in GitLab but that cause a Permission issue. It's is because of ssh pub key to upload)

    These steps work without using SSH

  • Create a repository/project in GitLab

  • I removed .git (that caused permission issue in previous. For to start with fresh)

  • git config --global user.name "user_name"

    git config --global user.email "user.email@gmail.com"

  • git init .

  • git remote add origin https://gitlab.com/user.account/user_project.git

  • git add . and git commit -m "initial commit"

  • git push -u origin master

    It will ask username and password. Then fixed.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •