Skip to content

Instantly share code, notes, and snippets.

@adamjohnson
Last active October 23, 2024 11:31
Show Gist options
  • Save adamjohnson/5682757 to your computer and use it in GitHub Desktop.
Save adamjohnson/5682757 to your computer and use it in GitHub Desktop.
Fix "Permission denied (publickey)" error when pushing with Git

"Help, I keep getting a 'Permission Denied (publickey)' error when I push!"

This means, on your local machine, you haven't made any SSH keys. Not to worry. Here's how to fix:

  1. Open git bash (Use the Windows search. To find it, type "git bash") or the Mac Terminal. Pro Tip: You can use any *nix based command prompt (but not the default Windows Command Prompt!)
  2. Type cd ~/.ssh. This will take you to the root directory for Git (Likely C:\Users\[YOUR-USER-NAME]\.ssh\ on Windows)
  3. Within the .ssh folder, there should be these two files: id_rsa and id_rsa.pub. These are the files that tell your computer how to communicate with GitHub, BitBucket, or any other Git based service. Type ls to see a directory listing. If those two files don't show up, proceed to the next step. NOTE: Your SSH keys must be named id_rsa and id_rsa.pub in order for Git, GitHub, and BitBucket to recognize them by default.
  4. To create the SSH keys, type ssh-keygen -t rsa -C "[email protected]". This will create both id_rsa and id_rsa.pub files.
  5. Now, go and open id_rsa.pub in your favorite text editor (you can do this via Windows Explorer or the OSX Finder if you like, typing open . will open the folder).
  6. Copy the contents--exactly as it appears, with no extra spaces or lines--of id_rsa.pub and paste it into GitHub and/or BitBucket under the Account Settings > SSH Keys. NOTE: I like to give the SSH key a descriptive name, usually with the name of the workstation I'm on along with the date.
  7. Now that you've added your public key to Github and/or BitBucket, try to git push again and see if it works. It should!

More help available from GitHub on creating SSH Keys and BitBucket Help.

@gowthamkumar12
Copy link

gowthamkumar12 commented Jun 12, 2023

Yeah, I have done all of that and I am also able to get this message "Hi gowthamkumar12! You've successfully authenticated, but GitHub does not provide shell access." while doing "ssh -T [email protected]"

@Divya0319
Copy link

Yeah, I have done all of that and I am also able to get this message "Hi gowthamkumar12! You've successfully authenticated, but GitHub does not provide shell access." while doing "ssh -T [email protected]"

For registering newly created SSH keys, you must register your keys (private keys) to SSH agent, instead of requesting shell access of GitHub.

@josehricardo
Copy link

This procedure solved my problem! thanks!!!

@tharaka16
Copy link

Worked like a charm. thank you!!

@bvercelli99
Copy link

Thanks!!

@Vil94
Copy link

Vil94 commented Aug 22, 2023

What a lifesaver, thank you!!

@iamkayshow
Copy link

Thanks it worked!

@pjkitsune
Copy link

Thank you so much! I was stumped!

@Yunishello
Copy link

Thanks so much it worked

@emil-raubach
Copy link

Thank you!

@ScorpQ
Copy link

ScorpQ commented Nov 1, 2023

Thank youuu

@roziqjon1
Copy link

l;,
l

@chronkituss
Copy link

Thank you, it worked!

@sagar03d
Copy link

sagar03d commented Jan 2, 2024

I was having this error on ubuntu, do not forget to add 'sudo' while running ssh-keygen command

@devonkinghorn
Copy link

I had this problem but only with docker-compose build with a git context. Git clone and push still worked though

For some reason when I ran ssh-add, it magically worked. No clue why but hopefully it helps someone.

$ ssh-add ~/.ssh/id_rsa

@0xkarambit
Copy link

Thank you bro, needed this

@Cegt25
Copy link

Cegt25 commented Apr 18, 2024

Gracias, esta solución me funciono perfectamente

@pxpana
Copy link

pxpana commented May 9, 2024

thank you mahn

@purplena
Copy link

extremely appreciated!

@hbolajraf
Copy link

For those who still have the issue, I created a step-by-step tutorial on how to fix the "Permission Denied (publickey)" error. Post : https://hbolajraf.net/posts/Connecting-to-GitHub-and-Pushing-Changes-Using-SSH-on-Windows/

@zcecchetti
Copy link

THIS WORKED!!!

@yongtaoer
Copy link

Thank you very much, your answer has helped me!

@Domjon24
Copy link

Thank you! This was very helpful & idiot-proof

@VishalPVijayan4
Copy link

Here's how you can proceed:

Check for SSH keys in PowerShell:
Run the following command to check the contents of the .ssh directory:

Get-ChildItem -Path $env:USERPROFILE\.ssh

  1. Copy the SSH public key:
    To copy the public key to the clipboard, run the following command in PowerShell:
    powershell
    Get-Content $env:USERPROFILE\.ssh\id_rsa.pub | clip
    This will copy the contents of your public key (id_rsa.pub) to your clipboard.

  2. Add the SSH key to your GitHub account:
    Go to GitHub and sign in.
    Navigate to Settings by clicking your profile picture at the top-right, then click SSH and GPG keys from the left sidebar.
    Click New SSH key.
    In the "Title" field, you can enter something like "My PC SSH Key".
    Paste the SSH key (copied in step 1) into the "Key" field.
    Click Add SSH key.

  3. Test the SSH connection:
    After adding the key to GitHub, test the connection by running this command in PowerShell:

bash
ssh -T [email protected]
You should see a message like this:

Hi username! You've successfully authenticated, but GitHub does not provide shell access.
4. Clone the repository using SSH:
Now you can clone the repository using the SSH URL:

bash
git clone [email protected]:your-username/your-repository.git
This should resolve the "Permission denied (publickey)" issue.

@aat007
Copy link

aat007 commented Oct 15, 2024

Many Thanks

@mukhusin
Copy link

Excellent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment