DEV Community

Geoffrey Kim
Geoffrey Kim

Posted on

How to Switch from HTTPS to SSH for GitLab Repositories

Switching from HTTPS to SSH for GitLab repositories can enhance your security and streamline your workflow. If you have already cloned your repository using HTTPS and want to switch to SSH, follow these steps:

1. Generate an SSH Key

First, generate an SSH key if you haven't already:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode

Replace your_email@example.com with your GitLab email. Press enter to accept the default file location and passphrase options.

2. Add Your SSH Key to GitLab

After generating the SSH key, add it to your GitLab account:

  1. Copy the public key to your clipboard:

    cat ~/.ssh/id_rsa.pub
    
  2. Log in to GitLab, navigate to Settings > SSH Keys.

  3. Paste the public key into the "Key" field.

  4. Add a title for the key and click Add key.

3. Change the Remote URL of Your Existing Repository

If you've already cloned your repository using HTTPS, you can change it to SSH with the following steps:

  1. In your terminal, navigate to your cloned repository:

    cd path/to/your/repository
    
  2. Change the remote URL to SSH:

    git remote set-url origin git@gitlab.com:username/repository.git
    

    Replace username/repository with your actual GitLab username and repository name.

  3. Verify the change:

    git remote -v
    

    The output should display the SSH URL for both fetch and push operations:

    origin  git@gitlab.com:username/repository.git (fetch)
    origin  git@gitlab.com:username/repository.git (push)
    

4. Push Changes Using SSH

Now you can use the usual Git commands to push changes using SSH:

git add .
git commit -m "Your commit message"
git push origin main
Enter fullscreen mode Exit fullscreen mode

Benefits of Using SSH

Using SSH keys provides a more secure and convenient way to authenticate with GitLab:

  • Security: SSH keys are cryptographically secure, reducing the risk of your credentials being compromised.
  • Convenience: Once set up, SSH keys allow you to push and pull from your repositories without entering your username and password every time.

Switching from HTTPS to SSH for your GitLab repositories is a simple process that can significantly enhance your development workflow. By following these steps, you can ensure a more secure and efficient method of interacting with your Git repositories.

Top comments (0)