You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Git推送至origin master遇publickey权限拒绝,如何重连GitHub?

解决GitHub SSH公钥权限拒绝问题的分步方案

Hey there, let's fix this git@github.com: Permission denied (publickey) headache once and for all—this is one of the most common GitHub SSH issues, and we can work through it step by step.

Step 1: Check if you already have an SSH key pair

First, let's see if you've got existing SSH keys on your machine. Run this in your terminal:

ls -al ~/.ssh

Look for files like id_ed25519 (private key) and id_ed25519.pub (public key)—Ed25519 is the modern, recommended key type. If you don't see these, or only have older RSA keys, generate a new pair with:

ssh-keygen -t ed25519 -C "your_email@example.com"

Just press Enter through all prompts (you can set a passphrase if you want extra security, but it's optional).

Step 2: Ensure the SSH agent is running and your key is added

SSH agent manages your keys in the background—let's make sure it's active and knows about your key:

  1. Start the agent:
    eval "$(ssh-agent -s)"
    
  2. Add your private key to the agent:
    ssh-add ~/.ssh/id_ed25519
    
    (If you're using an RSA key, replace id_ed25519 with id_rsa.)
    Pro tip: If you tried adding the key before and it didn't stick, run ssh-add -d ~/.ssh/id_ed25519 first to remove any old entries, then re-add.

Step 3: Add your public key to your GitHub account

This is the most crucial step—GitHub needs to recognize your machine's public key:

  1. Copy your public key to your clipboard:
    cat ~/.ssh/id_ed25519.pub
    
    Select and copy the entire output (it starts with ssh-ed25519 and ends with your email).
  2. Log into GitHub, go to Settings > SSH and GPG keys > New SSH key.
  3. Give it a descriptive title (like "My Work Laptop") and paste the copied key into the "Key" field. Click Add SSH key.

Step 4: Test the SSH connection

Let's verify everything works before trying to push again:

ssh -T git@github.com

If you see a message like Hi [YourUsername]! You've successfully authenticated, but GitHub does not provide shell access., you're good to go!

Bonus Troubleshooting Tips (if the above steps don't work)

  • Check your remote repo URL: Run git remote -v—if your origin is using HTTPS instead of SSH, switch it with:
    git remote set-url origin git@github.com:YourUsername/YourRepoName.git
    
  • Fix ~/.ssh/config issues: If you have a ~/.ssh/config file, make sure your GitHub entry looks like this (adjust the key path if needed):
    Host github.com
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_ed25519
    
  • Disable VPN/Firewall temporarily: Sometimes VPNs or strict firewalls block SSH connections to GitHub—try turning them off briefly to test.
  • Confirm repo permissions: Double-check that your GitHub account has push access to the repo (you're either the owner or added as a collaborator with write permissions).

内容的提问来源于stack exchange,提问作者Batuhan Dogan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:43:50