Generate and Configure a Git SSH key
You can connect to GitHub using either HTTPS or SSH. Both methods are secure, but they differ in how they handle authentication:
-
HTTPS uses a personal access token (PAT) or your GitHub credentials, which can be stored securely using a credential manager (e.g., Windows Credential Manager, macOS Keychain, or Git Credential Helper on Linux).
-
SSH uses a pair of cryptographic keys (private and public) for authentication, eliminating the need for credentials or tokens once configured.
For most developers, SSH is preferred because it provides a simple, password-free workflow and makes it easy to manage multiple GitHub accounts or devices.
1. Generate an SSH key
To generate a new SSH key pair, run:
If the ~/.ssh directory doesn't exist, create it first:
When prompted:
- Use a descriptive filename, for example:
id_ed25519_github - Enter a secure passpharase (recommended)
2. Add the SSH key to GitHub
Once the key pair is generated, display your public key:
Copy the output and add it to your GitHub account:
Then in your GitHub page:
- Go to GitHub -> Settings -> SSH and GPG keys
- Click New SSH key
- Fill the fields as follows:
- Title: a short label for your device (e.g., workstation-linux, macbook-personal)
- Key Type: Authentication Key
- Key: paste the copied public key
- Click Add SSH key to save it
3. Configure SSH for GitHub
Tell SSH which key to use when connection to GitHub by editing or creating
the file ~/.ssh/config:
# GitHub account
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_ed25519_github
To test the connection:
If everything is configured correctly, you should see a message like:
Reference Material
Git
GitHub