In this article we are going to cover How to Create SSH Key in Git, How to Add SSH key to GitLab, Clone Gitlab Repo with SSH and Push Local code to GitLab repository.
Table of Contents
What is SSH Key ?
SSH stands for Secure Shell. And it is authenticates to the GitLab server without using username and password each time. And before creating SSH Key you need to install Git on your machine.
Follow below link install git client on Windows or Linux OS
install Git client on as per your OS Environment
Step #1: Open the Git Bash and run the command to run generate SSH key in Git
ssh-keygen
![How to Add SSH key to GitLab [2 Steps] 1](https://www.fosstechnix.com/wp-content/uploads/2021/08/ssh-keygen_1.png)
Step #2: Now go to GitLab.com and login to your GitLab account and click on profile, click on Edit Profile
Step #3: Now click on SSH Keys at left side of menu
![How to Add SSH key to GitLab [2 Steps] 3](https://www.fosstechnix.com/wp-content/uploads/2021/08/click-on-SSH-Keys_3-1024x415.png)
Step #4: Go to C:\ drive, click on .ssh folder and open the file with .pub extension and copy the SSH key
![How to Add SSH key to GitLab [2 Steps] 4](https://www.fosstechnix.com/wp-content/uploads/2021/08/click-on-.ssh-folder_4.png)
Step #5: Add SSH key to GitLab Profile
![How to Add SSH key to GitLab [2 Steps] 5](https://www.fosstechnix.com/wp-content/uploads/2021/08/paste-ssh-key-in-gitlab_5-1024x375.png)
Step #6: Now click on Add key button and add SSH key in GitLab
![How to Add SSH key to GitLab [2 Steps] 6](https://www.fosstechnix.com/wp-content/uploads/2021/08/add-SSH-key-in-GitLab_6-1024x338.png)
Here you can see our SSH Key has been created successfully.
Step #7: How to Clone Gitlab Repo with SSH
Before Push code firstly you have to config the username and email as shown in below
git config --global user.name "devopshint"
git config --global user.email [email protected]
git config –list
![How to Add SSH key to GitLab [2 Steps] 7](https://www.fosstechnix.com/wp-content/uploads/2021/08/config-the-username-and-email_7.png)
Open Git Bash and run the command git clone and your clone with SSH same as shown below
git clone [email protected]:devopshint/demo.git
![How to Add SSH key to GitLab [2 Steps] 8](https://www.fosstechnix.com/wp-content/uploads/2021/08/git-clone-and-your-clone-with-SSH_8.png)
List the repo if it is cloned on your system
ls
![How to Add SSH key to GitLab [2 Steps] 9](https://www.fosstechnix.com/wp-content/uploads/2021/08/clone-gitlab-repo_9.png)
Step #8: Push Local code to GitLab Repo
Navigate to git repo
cd demo
Create one file or copy any file that repository folder for creating file run the command touch and filename
then run the command git add .
git add.
Run the command git commit –m “you can assign any commit here”.
git commit –m “you can assign any commit here”
Run the push command git push –u origin main(main is the branch name if your branch name is master then you have to run the command main)
Conclusion:
We have covered How to Create SSH Key in Git, How to Add SSH key to GitLab, Clone Gitlab Repo with SSH and Push Local code to GitLab repository
Related Articles:
Reference:
do you have Mac instructions?