The default method of pushing to Gitlab, Github, and other repository hosts is via HTTPS. It is a secure method but the downside—atleast for me—is that you will be prompted to enter your login details to Gitlab/Github before pushing. This is why my preferred method of uploading files to Gitlab is SSH.
In a previous article, I went over how to login into a remote host via SSH. Uploading to a remote host, in this case our git repo host, is no different except that rather than manually logging into the host and uploading files, it’s done for us automatically by git and the appropriate SSH Keys.
The first step is to create some SSH keys for our machine. The ‘ssh-keygen’ command is a tool that allows us to generate ssh keys and store them in the directory of our choosing. Here is an example:
The tool asks you to enter the path where you want to store the ssk keys, prompts you to secure it(I usually don’t out of habit but maybe you should), and then it saves the keys in the path you entered. At this stage, all the hard work is done.
How to upload the keys to Gitlab.
Once you have generated the SSH Keys:
- Login to you Gitlab account.
- Navigate to your profile settings
- Find the SSH Keys tab in the left panel
- Go to the path where you stored your SSH Keys on your machine
- Copy the public key and paste it into Gitlab(The public key has a .pub extension)
- Save the keys then change your git remote to use SSH
- Then when you push, Gitlab will authenticate the machine automatically and you’ll be able to upload your files without any login prompts