Sunday, January 5, 2025

Use SSH Keys to clone GIT Repository using SSH

 

1. Generate a New SSH Key Pair

bash

ssh-keygen -t rsa -b 4096 -C "HSingh@MindTelligent.com"
  • -t rsa specifies the type of key (RSA in this case).
  • -b 4096 sets the number of bits for the key length (4096 is more secure).
  • -C "HSingh@MindTelligent.com" adds a comment (usually your email) to help identify the key.

2. Save the Key Files

  • You will be prompted to enter a file name and location to save the key pair:
    bash

    Enter file in which to save the key (/home/user/.ssh/id_rsa):
    • Press Enter to save it in the default location (~/.ssh/id_rsa).
    • Or specify a custom path if you want multiple keys.

3. Set a Passphrase (Optional)

You will be asked:



Enter passphrase (empty for no passphrase):
  • Enter a passphrase for extra security, or press Enter for no passphrase.

4. View the Public Key

bash

cat ~/.ssh/id_rsa.pub

This will display the public key, which you can copy to add to remote servers or platforms like GitHub, GitLab, or AWS.


5. Add Key to SSH Agent (Optional for Convenience)

bash

eval "$(ssh-agent -s)" # Start the SSH agent
ssh-add ~/.ssh/id_rsa # Add the key to the agent

 

6. Add SSH Key to Git Hosting Provider

  • GitHub: Go to Settings → SSH and GPG keys → New SSH Key and paste the contents of your public key:
bash

cat ~/.ssh/id_rsa.pub
  • GitLab/Bitbucket: Follow similar steps under SSH Keys settings.

7. Test SSH Connection

Test the SSH connection to your hosting provider:

  • For GitHub:
bash

ssh -T git@github.com
  • For GitLab:
bash

ssh -T git@gitlab.com

You should see a success message, e.g.:

rust
Hi username! You've successfully authenticated.

8. Clone Repository Using SSH

Copy the SSH URL of the repository from the hosting provider. It looks like:

scss
git@github.com:username/repo.git

Then, clone it:

bash
git clone git@github.com:username/repo.git

Use SSH Keys to clone GIT Repository using SSH

  1. Generate a New SSH Key Pair bash ssh-keygen -t rsa -b 4096 -C "HSingh@MindTelligent.com" -t rsa specifies the type of key (...