SSH keys are not just for Git but if you want to use SSH cloning for git, yeah you need em.
To create a new SSH key pair do the following:
1.) Open a terminal on Linux or macOS, or Git Bash / WSL on Windows.
2.) Generate a new ED25519 SSH key pair:
ssh-keygen -t rsa -b 2048 -C "firstname.lastname@example.org" or ssh-keygen -t ed25519 -C "email@example.com"
3.) Use the defaults for all options if you like. Doesn’t matter.
Copying SSH Key to Gitlab
Go into the ~/.ssh folder. On Mac you may need to do the following to see the .ssh folder:
# open the finder dialog Command+Shift+G # enter ~/.ssh # view hidden files Command+Shift+.
cd ~/.ssh # on your keyboard hit Ctrl +h
Once you can see the hidden files you should see a file named id_rsa.pub or something like that. It ends with .pub. Open that file with a text editor and you will see the SSH key you need to copy to your own gitlab account.
Using a Access Token (works too but yuk!)
If you are not using an SSH connection you may need to create a personal access token (image below). Make sure you save the token on your local machine as you will not be able to retreive it once you close the page where you created it on Gitlab.
To clone a repo using an access token, it is similar to cloning with https but the url is slightly different. If your token is for example xSx81KqnADs-mZ4JviHa, the cloning command will be
git clone https://oauth2:xSx81KqnADs-mZ4JviHa@gitlab.com/myaccount/myrep.git
If you were previously using https with a username and password, you will need to update the remote url on your local machine. Once you have created the access token you will need to change the remote origin of your local repo to add the access token. Here is an example of the old remote url and a new one
# old url https://somegitsite.com/mycompany/mysite.git # new url with access token https://oauth2:uggU-s2usayJtiqguEAQ@somegitsite.com/mycompany/mysite.git
To set the remote url use the following command as an example:
git remote set-url origin https://oauth2:AmDAyXHEVxyEBf3fbg@somegitsite.com/mycompany/mysite.git