I know, I'm answering this very late and even StackOverflow confirmed if I really want to answer. I'm answering because no one actually described the actual problem so wanted to share the same.
The Basics
First, understand that what is the remote here. Remote is GitLab and your system is the local so when we talk about the remote origin
, whatever URL is set in your git remote -v
output is your remote URL.
The Protocols
Basically, Git clone/push/pull works on two different protocols majorly (there are others as well)-
- HTTP protocol
- SSH protocol
When you clone a repo (or change the remote URL) and use the HTTPs URL like https://gitlab.com/wizpanda/backend-app.git then it uses the first protocol i.e. HTTP protocol.
While if you clone the repo (or change the remote URL) and uses the URL like [email protected]:wizpanda/backend-app.git
then it uses the SSH protocol.
HTTP Protocol
In this protocol, every remote operation i.e. clone, push & pull uses the simple authentication i.e. username & password of your remote (GitLab in this case) that means for every operation, you have to type-in your username & password which might be cumbersome.
So when you push/pull/clone, GitLab/GitHub authenticate you with your username & password and it allows you to do the operation.
If you want to try this, you can switch to HTTP URL by running the command git remote set-url origin <http-git-url>
.
To avoid that case, you can use the SSH protocol.
SSH Protocol
A simple SSH connection works on public-private key pairs. So in your case, GitLab can't authenticate you because you are using SSH URL to communicate. Now, GitLab must know you in some way. For that, you have to create a public-private key-pair and give the public key to GitLab.
Now when you push/pull/clone with GitLab, GIT (SSH internally) will by default offer your private key to GitLab and confirms your identity and then GitLab will allow you to perform the operation.
So I won't repeat the steps which are already given by Muhammad, I'll repeat them theoretically.
- Generate a key pair `ssh-keygen -t rsa -b 2048 -C "My Common SSH Key"
- The generated key pair will be by default in
~/.ssh
named id_rsa.pub
(public key) & id_rsa
(private key).
- You will store the public key to your GitLab account (the same key can be used in multiple or any server/accounts).
- When you clone/push/pull, GIT offers your private key.
- GitLab matches the private key with your public key and allows you to perform.
Tips
You should always create a strong rsa key with at least 2048 bytes. So the command can be ssh-keygen -t rsa -b 2048
.
https://gitlab.com/help/ssh/README#generating-a-new-ssh-key-pair
General thought
Both the approach have their pros & cons. After I typed the above text, I went to search more about this because I never read something about this.
I found this official doc https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols which tells more about this. My point here is that, by reading the error and giving a thought on the error, you can make your own theory or understanding and then can match with some Google results to fix the issue :)
ssh -vvvv [email protected]
to see if it picks up the SSH keysudo git clone [email protected]:project/somethiing.git
, otherwise ssh will look in/root/.ssh
instead of the key you uploaded~/.ssh/id_rsa