Subscribe & Follow

How to Clone Git Repositories with SSH and Convert HTTPS to SSH

How to Clone Git Repositories with SSH and Convert HTTPS to SSH

Git is a popular version control system that allows developers to collaborate on projects efficiently. It offers multiple methods for authentication, including HTTPS and SSH. While HTTPS is commonly used due to its simplicity, SSH provides added security and convenience once set up correctly. In this blog, we'll guide you through the process of cloning a Git repository using SSH and converting an existing HTTPS repository to SSH.


  1. Clone Gitlab Repo With SSH 

Cloning and setting up a GitLab repository using SSH involves a few steps. Below is a step-by-step example of how to do this: 


Step 1: Generate SSH Key (if you don't have one): 

If you don't have an SSH key set up, you need to generate one. Open a terminal or command prompt and run the following command: 


ssh-keygen -t rsa -b 4096 -C "your_email@example.com" 


This will generate a new SSH key pair. When prompted, you can choose the location to save the key (usually, it's saved in the default location, like ~/.ssh/id_rsa).

 

Step 2: Add SSH Key to GitLab: 

Copy the public key to your clipboard using the following command: 


cat ~/.ssh/id_rsa.pub 


Next, log in to your GitLab account, go to "Settings" > "SSH Keys" and paste the public key into the "Key" field. Give the key a title and click "Add key" to save it. 


Step 3: Clone the GitLab Repository: 

Now, you can clone the GitLab repository using SSH. Open a terminal or command prompt, navigate to the directory where you want to clone the repository, and run the following command: 


git clone git@gitlab.com:yourusername/your-repo.git 


Replace yourusername with your GitLab username and your-repo with the name of your repository. 


Step 4: Configure Git Identity: 

Set up your Git identity for this repository: 


cd your-repo 

git config user.name "Your Name" 

git config user.email "your_email@example.com" 


Replace "Your Name" with your name and "your_email@example.com" with the email associated with your GitLab account. 


Step 5: Verify Remote Origin: You can check the remote URL of your repository to ensure it's using SSH:


git remote -v 


The output should show the SSH URL (e.g., git@gitlab.com:yourusername/your-repo.git). 


Step 6: Pull and Push Changes: Now you can pull the latest changes from the repository and push your changes using the regular Git commands: 


git pull origin master # Make your changes to the code 

git add . 

git commit -m "Your commit message" 

git push origin master 


Remember to replace master with the appropriate branch if you're working on a different branch. 


That's it! You have successfully cloned and set up a GitLab repository using SSH. You can now work with the repository using Git commands and enjoy the benefits of secure and authenticated communication with the GitLab server. 


  1. Convert HTTPS to SSH 

Converting an existing GitLab repository from an HTTPS connection to an SSH connection involves removing the HTTPS remote and adding an SSH remote. Here's a step-by-step guide to do this: 


Step 1: Check Current Remote URL: First, let's check the current remote URL of your repository using the following command: 


git remote -v 


This will show you the current remote URL for both fetch and push operations. 


Step 2: Remove HTTPS Remote: 

To remove the HTTPS remote, use the following command: 


git remote remove origin


This will remove the remote called "origin" (assuming it's named that way). 


Step 3: Get SSH Remote URL: 

Next, you'll need the SSH URL of your GitLab repository. Go to your GitLab repository page, click the green "Code" button, and choose the "SSH" option. Copy the provided SSH URL. 


Step 4: Add SSH Remote: 

Now, add the SSH remote using the following command:


git remote add origin git@gitlab.com:yourusername/your-repo.git 


Replace yourusername with your GitLab username and your-repo with the name of your repository. This command sets the SSH URL as the new remote for both fetching and pushing. 


Step 5: Verify SSH Remote: To verify that you've set up the SSH remote correctly, use the following command: 


git remote -v

 

The output should show the new SSH URL as the remote origin (e.g., 

git@gitlab.com:yourusername/your-repo.git).


Step 6: Push Changes (Optional): 

If you already have some local changes in your repository, you might want to push them to the new SSH remote: 


git push origin master 


Replace master with the appropriate branch name if you're working on a different branch. 


That's it! You have successfully converted your GitLab repository from an HTTPS connection to an SSH connection. From now on, you can use SSH for all your interactions with the remote repository, providing a more secure and authenticated way of communication.


Using SSH with Git provides enhanced security and ease of access, especially when collaborating on projects. By generating an SSH key pair, you can clone repositories using SSH and even switch from HTTPS to SSH in your existing repositories. Whether you're starting a new project or optimizing an existing one, embracing SSH for your Git interactions is a valuable skill for any developer. Happy coding!

Leave a Comment

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