13

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.

beliz
  • 402
  • 1
  • 5
  • 25
  • 2
    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 08 '20 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 08 '20 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 08 '20 at 22:01
  • I already assigned my ssh key to gitlab and it is working in the other repository... @NaorLevi – beliz Jan 08 '20 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 08 '20 at 22:13

4 Answers4

11

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

Also

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    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 08 '20 at 22:13
  • 1
    @beliz The other repo wouldn't have an HTTPS URL by any chance? (check the `git remote -v` output) – VonC Jan 08 '20 at 22:14
  • oh no.. it has that. how do I fix this and make everything work? – beliz Jan 08 '20 at 22:15
  • 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 08 '20 at 22:18
  • @beliz I have edited the answer with the right command to generate a proper SSH key. But save/copy the old one first. – VonC Jan 08 '20 at 22:21
  • 1
    @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:/`. – VonC Jan 08 '20 at 22:23
  • @VonC 1. How I can check if SSH work properly ? 2. my problem not with existed repository, but with their cloning. command you mentioned above nothing changed! – Dmitriy Ogureckiy Jan 21 '23 at 14:24
  • @DmitriyOgureckiy `ssh -Tv git@gitlab.com` should give you a message with your GitLab accuont name in it: that will confirm SSH is working properly. The command mentioned creates a new SSH key, but you still need to [add its public key to your GitLab profile](https://docs.gitlab.com/ee/user/ssh.html#add-an-ssh-key-to-your-gitlab-account). Then you will be able to clone repositories using SSH URLs. – VonC Jan 21 '23 at 20:45
5

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
  1. 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
    
    

The error while trying to push the code

  1. 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.

The SSH Key on working directory

  1. 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"

The output of Git Bash

  1. We should put the generated keys from the working folder into this folder

The folder path in which image has to be copied

  1. 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

  2. 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

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

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

the code will be pushed.

the new key is read and pushed in Git

venkat
  • 51
  • 1
  • 2
4

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

  1. Create a repository/project in GitLab

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

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

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

  4. git init .

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

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

  7. git push -u origin master

It will ask username and password. Then fixed.

Raikumar Khangembam
  • 948
  • 1
  • 12
  • 24
1

< Git looks at ssh keys by name, picking the ones starting with id_ first... In case there are two ssh keys with names starting with id_ it tries both and see if one of them is fine. If your ssh key name starts with a different name you need to change it.

Example:

keyssh (private key) change the name to--> id_rsa (private key)
keyssh.pub (public key) change the name to--> id_rsa.pup (public key)

< I also created a file named "config" in the .ssh folder.
The "config" file has no extension and is needed for correct configuration/management of the ssh key:

enter image description here

< Also, I added the login credentials and the network domain in the windows credential manager:

Internet or network address: https://corporate-gitlab.xxxxx.com
Username: username@xxxxxxxxxxxx.com
Password: ••••••••••

This way I solved the error:

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

Please make sure you have the correct access rights
and the repository exists.
Cryptoer17
  • 61
  • 5