Setup local Linux git repo to push to GitLab using SSH

If you or your organisation are using GitLab then here are a few steps that you might find useful when creating a repo and pushing it to GitLab.

Firstly create your repo on GitLab from the screen below:

git-gitlab-1

After that your remote repo will be created. We now need to setup git on your dev machine.

The steps below are just for linux as this tutorial is for Linux, windows is covered here(to be completed!).

Setup a git user

git config –global user.name “your name
git config –global user.email “you@youremail.com

Create Empty Repository

If you have an existing repo skip this stage.

SSH URLs are strongly recommended.
You will need to add an SSH key for each platform on which you will use git.
cd to your directory where you wish to store you git repo. Create a directory for this project.

mkdir your-project

cd your-project
git init
touch README
git add README
git commit -m ‘first commit’
git remote add origin git@git.domain:group/your-project.git

Existing Git Repo?

cd existing_git_repo
git remote add origin git@git.domain:group/your-project.git
git push -u origin master

 

Adding SSH key

Before generating an SSH key, check if your system already has one by running:

cat ~/.ssh/id_rsa.pub

If your see a long string starting with ssh-rsa or ssh-dsa, you can skip the ssh-keygen step.

To generate a new SSH key just open your terminal and use code below. The ssh-keygen command prompts you for a location and filename to store the key pair and for a password. When prompted for the location and filename you can press enter to use the default. It is a best practice to use a password for an SSH key but it is not required and you can skip creating a password by pressing enter. Note that the password you choose here can’t be altered or retrieved.

ssh-keygen -t rsa -C "you@youremail.com"

Use the code below to show your public key.

cat ~/.ssh/id_rsa.pub

Copy-paste the key (copy the complete key starting with ssh- and ending with your username and host).

 

Navigate to your SSH keys section in your profile on GitLab. Select your profile icon, then the SSH tab  and then Add Key, as highlighted below:

git-gitlab-2

Paste the key into the key text field that appears when you click “Add Key”

Your first push

Now go back to your local git repo directory and run the git push command on the command line. It will ask for your password that you used to create the key.

git push -u origin master

You should now be able to check on GitLab that a commit and push has been completed.

Leave a Reply

Your email address will not be published. Required fields are marked *