Check the docs tutorial in here: https://docs.gitlab.com/ee/user/ssh.html
These are the main steps:
# the "-C" parameter is a comment to remind you about this key
ssh-keygen -t ed25519 -C "<YOUR_NAME> at gitlab" -f $HOME/.ssh/my_gitlab_eddsaAs an alternative for e.g. RSA (both -P and -N are for password and new password):
ssh-keygen \
-q \
-t rsa \
-b 3072 \
-f ~/.ssh/id_gitlab_rsa_3072 \
-P "" -N "" -C ""You should now have the key pair as:
$HOME/.ssh/my_gitlab_eddsa$HOME/.ssh/my_gitlab_eddsa.pub
Reference: https://gitlab.com/help/ssh/README#generating-a-new-ssh-key-pair
eval $(ssh-agent -s)
ssh-add ~/.ssh/my_gitlab_eddsa
touch ~/.ssh/config
echo "Host gitlab.com" >> ~/.ssh/config
echo " Preferredauthentications publickey" >> ~/.ssh/config
echo " IdentityFile ~/.ssh/my_gitlab_eddsa" >> ~/.ssh/configReference: https://gitlab.com/help/ssh/README#working-with-non-default-ssh-key-pair-paths
Add the public key to the website (Github, Gitlab, etc.).
# copy to the clipboard the public key,
# then go to the website with the browser to manually add what you just copied.
xclip -sel clip < ~/.ssh/my_gitlab_eddsa.pubThen you can check the SSH key pair works as expected:
ssh -T [email protected]ReferenceL https://gitlab.com/help/ssh/README#adding-an-ssh-key-to-your-gitlab-account
When using private repositories (e.g. on GitLab) this extra global configuration for git needs to be added.
This is going to be used by Go (Golang) when running go get and go mod for Go Modules.
git config \
--global \
url."https://${GITLAB_TOKEN_KEY}:${GITLAB_TOKEN_VALUE}@gitlab.com".insteadOf \
"https://gitlab.com"If a global config entry needs to be removed, then use:
git config --global --unset url.https://${GITLAB_TOKEN_KEY}:${GITLAB_TOKEN_VALUE}@gitlab.com.insteadofWhen in need to update a Go module dependency for a private git repository targeting a specific git branch (not master) you can run this:
go get gitlab.com/${MY_PRIVATE_REPOSITORY}@${MY_BRANCH}This will update go.mod and go.sum to the HEAD commit ID for that particular git branch (${MY_BRANCH}).