1. Install Git
Download and install Git based on your operating system:
- Windows: Download Git for Windows
- macOS/Linux: Use your system’s package manager.
macOS/Linux Installation Commands
Confirm Git Installation
2. Generate an SSH Key Pair
Generate a new SSH key pair to securely connect to GitLab or GitHub.
SSH Key Generation Command
- Replace
your.email@example.com
with your email associated with GitLab or GitHub. - Save the key in the default location:
- Windows:
C:\Users\<YourUsername>\.ssh\id_rsa
- macOS/Linux:
~/.ssh/id_rsa
- Windows:
Optional Passphrase Setup
Set a passphrase during key generation for enhanced security.
3. Add the SSH Key to the SSH Agent
Start the SSH agent and add your private key.
Start SSH Agent
Different platforms have different ways to start SSH Agent.
SSH Agent on Windows (Git Bash)
SSH Agent on macOS
For macOS 10.12 and later:
SSH Agent on Linux
Add the SSH Key to the Agent
This code depends on the SSH dependencies installed and correctly configured and might not work on Windows.
4. Add the SSH Key to GitLab or GitHub
Copy your public key and add it to your GitLab or GitHub account.
Copy the Public Key
Add Key to GitLab
- Log in to GitLab.
- Navigate to Preferences > SSH Keys.
- Paste the public key and click Add key.
Add Key to GitHub
- Log in to GitHub.
- Navigate to Settings > SSH and GPG keys > New SSH key.
- Paste the public key and click Add SSH key.
5. Test the SSH Connection
Verify the SSH connection to ensure everything is configured correctly.
Test Connection for GitLab
Expected output:
Welcome to GitLab, <username>!
Test Connection for GitHub
Expected output:
Hi <username>! You've successfully authenticated.
Notes
Some things to consider and keep in mind.
Default SSH Key Path
Ensure the SSH key is saved in the default location (~/.ssh/id_rsa
). If using a custom path, specify it when adding the key to the SSH agent or connecting.
Multiple SSH Keys Configuration
Use an ~/.ssh/config
file to manage multiple SSH keys.
Example configuration: