SSH (Secure Shell) authentication is a secure and efficient way to connect to GitHub repositories without entering your username and password for every operation.
Encountering the "Permission denied (publickey)" error while trying to interact with GitHub can be frustrating, especially when you're in the middle of critical development work.
This error occurs when Git cannot authenticate your SSH key with GitHub, often due to a misconfigured SSH setup or missing keys. By the end of this article, you'll be back to seamless Git operations without the hassle.
This error occurs because SSH authentication isn't set up for GitHub. Let's fix this by setting up SSH keys:
- First, check if you have existing SSH keys:
- If you don't have keys (or want new ones), generate them:
ssh-keygen -t ed25519 -C "your_github_email@example.com"
# When prompted: # - Press Enter to accept the default file location # - Enter a passphrase (or press Enter twice for no passphrase)
- Start the SSH agent:
- Add your SSH key to the agent:
- Copy your public key:
pbcopy < ~/.ssh/id_ed25519.pub
- Add the key to GitHub:
- Go to GitHub.com and log in
- Click your profile photo β Settings
- Click "SSH and GPG keys" in the sidebar
- Click "New SSH key" or "Add SSH key"
- Give it a title (e.g., "My Mac")
- Paste the key you copied
- Click "Add SSH key"
- Test your SSH connection:
- Now try pushing again:
git config --global user.name "Your Name"
git config --global user.email "your_github_email@example.com"
# Then add your remote and push
git remote add origin git@github.com:YOUR_USERNAME/media-kit.git
git branch -M main
git push -u origin main
- If you prefer to use HTTPS instead of SSH:
git remote remove origin
# Add the HTTPS remote URL
git remote add origin https://github.com/YOUR_USERNAME/media-kit.git
git branch -M main
git push -u origin main
When using HTTPS, you'll need to:
- Enter your GitHub username
- For password, use a Personal Access Token:
- Go to GitHub β Settings β Developer settings β Personal access tokens
- Generate new token (classic)
- Select repo scope
- Copy the token and use it as your password
By following the steps outlined in this article, you've tackled one of the most common SSH-related issues developers face when working with GitHub. With your SSH keys properly configured and your authentication issues resolved, you're now equipped to handle Git like a pro.
Git no longer holds any secrets for youβevery push, pull, and clone will work seamlessly. Keep exploring, coding, and mastering Git, knowing youβve unlocked its full potential.