How to: Connect SSH to git Emman, November 11, 2023November 11, 2023 To connect to Git repositories using SSH, you need to follow these steps: 1. Generate SSH Key Pair: If you don’t have an SSH key pair, you need to generate one. Open a terminal and run the following command: ssh-keygen -t rsa -b 4096 -C "your_email@example.com" This will create a new SSH key pair. Follow the prompts and provide a passphrase if you want. 2. Add SSH Key to the SSH Agent: If you’ve generated an SSH key with a passphrase, it’s a good idea to add it to the SSH agent to avoid entering the passphrase every time you use the key. Run: ssh-add path/to/your/private/key 3. Copy the Public Key: You need to copy the public key to your clipboard. You can use the following command to display the key: cat ~/.ssh/id_rsa.pub Copy the entire output, including the “ssh-rsa” at the beginning. 4. Add SSH Key to Your Git Provider: GitHub Go to your GitHub account settings. Click on “SSH and GPG keys.” Click on “New SSH key.” Paste your public key into the “Key” field and add a meaningful title. Click “Add SSH key.” GitLab: Go to your GitLab account settings. Click on “SSH Keys” in the left sidebar. Paste your public key into the “Key” field and add a title. Click “Add Key.” Bitbucket: Go to your Bitbucket account settings. Click on “SSH keys” in the left sidebar. Click “Add key.” Paste your public key into the “Key” field and give it a label. Click “Add key.” 5. Test the SSH Connection: To test if your SSH connection is working, run: ssh -T git@github.com Replace “github.com” with your Git provider’s hostname. If it’s successful, you should see a message like: Hi username! You've successfully authenticated, but GitHub does not provide shell access. 6. Configure Git to Use SSH: Now, configure Git to use SSH for your repositories. If you have an existing repository, you can update the remote URL: git remote set-url origin git@github.com:username/repo.git Replace “github.com:username/repo.git” with your repository URL. If you are cloning a new repository, use the SSH URL: git clone git@github.com:username/repo.git Replace “github.com:username/repo.git” with your repository URL. Now, you should be able to interact with your Git repositories using SSH. Make sure to use the SSH URL when working with repositories that you’ve configured for SSH access. Share this:FacebookX Related Discover more from Code Concepts Snippets Subscribe to get the latest posts sent to your email. Type your email… Subscribe Dev Developer Git SSH