SSH (Secure Shell) connections are the recommended authentication method for GitHub development, as they are more secure than HTTPS and eliminate the need to enter a password each time.
1. Generating an SSH Key Pair
SSH connections require a pair of public and private keys. The private key is stored securely on your local machine, and the public key is registered with GitHub.
Open a terminal and run one of the following commands to generate a key pair. Replace "your_email@example.com" with your GitHub-registered email address.
- Ed25519 (Recommended): A newer, more secure, and faster algorithm.
ssh-keygen -t ed25519 -C "your_email@example.com" - RSA (Legacy):
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
After running the command, you will be prompted for the key pair storage location and a passphrase.
- Storage location: Press Enter to use the default location (
~/.ssh/id_ed25519or~/.ssh/id_rsa).> Enter a file in which to save the key (/Users/you/.ssh/id_ed25519): [Press enter] - Passphrase: A password to protect your private key. Setting one is strongly recommended for security. You can leave it empty, but this allows SSH connections without a passphrase.
> Enter passphrase (empty for no passphrase): [Type a passphrase] > Enter same passphrase again: [Type passphrase again]
Once generated, the private key (e.g., id_ed25519) and public key (e.g., id_ed25519.pub) will be created in the specified directory.
Copying the Public Key
Copy the contents of the generated public key to your clipboard.
- macOS:
pbcopy < ~/.ssh/id_ed25519.pub # or pbcopy < ~/.ssh/id_rsa.pub - Linux (with xclip installed):If xclip is not installed, display the contents with
xclip -sel clip < ~/.ssh/id_ed25519.pub # or xclip -sel clip < ~/.ssh/id_rsa.pubcat ~/.ssh/id_ed25519.puband copy manually.
2. Registering the Public Key on GitHub
- Log in to GitHub, click your profile icon in the top right, and select “Settings”.
- Select “SSH and GPG keys” from the left sidebar.
- Click the “New SSH key” or “Add SSH key” button.
- In “Title”, enter a descriptive name for this SSH key (e.g.,
My MacBook Pro). - In the “Key” field, paste the public key contents you copied to the clipboard.
- Click “Add SSH key” to complete the registration.
3. Testing the SSH Connection
Verify that the SSH connection to GitHub is properly configured.
ssh -T git@github.com
If this is your first time connecting, you may be asked to confirm the host’s fingerprint. Type yes to continue.
The authenticity of host 'github.com (IP address)' can't be established.
RSA key fingerprint is SHA256:...
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
If you see the following message, your SSH connection is successful:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
username will be replaced with your GitHub username.
4. Changing Remote Repository URL to SSH Format
If your existing local repository was cloned via HTTPS, you need to change the remote URL to SSH format.
Navigate to your repository directory and run the following command:
cd [your-repository-path]
git remote set-url origin git@github.com:[your-github-username]/[repository-name].git
Example: git remote set-url origin git@github.com:yuhi-sa/my-awesome-repo.git
Now git pull and git push operations will be performed over SSH.