Push to GitHub without a password using ssh-key
Asked Answered
T

9

368

I generated an SSH key pair without a password and added the public key to GitHub.

Connection with

user@dev:/var/www/project# ssh -T [email protected]
Hi User! You've successfully authenticated, but GitHub does not provide shell access.

was successful and when I rename the key, it fails.

But when I want to push my changes, it stills ask me for my username and password combination.

Is there a way to push without a password?

Tieratierce answered 7/2, 2013 at 22:13 Comment(2)
Make sure you are not using https://github... in your remotes. They should also follow the git@github... format.Alannaalano
about-remote-repositories two type: 1. HPPT like https://github.com/user/repo.git 2. SSH, like [email protected]:user/repo.gitFission
H
728

If it is asking you for a username and password, your origin remote is pointing at the HTTPS URL rather than the SSH URL.

Change it to ssh.

For example, a GitHub project like Git will have an HTTPS URL:

https://github.com/<Username>/<Project>.git

And the SSH one:

[email protected]:<Username>/<Project>.git

You can do:

git remote set-url origin [email protected]:<Username>/<Project>.git

to change the URL.

Hotien answered 8/2, 2013 at 4:46 Comment(3)
This solved it, but I keep asking myself why would Github then advise you to point a new repository's remote end to an http URL by default. I just created a repository from scratch, and I was presented with an option for setting an https remote URL, not a git one.Bulge
Here's a quick one-liner shell command that will automatically change your https url to the appropriate git one (Only works for github urls!): git remote set-url origin $(git remote show origin | grep "Fetch URL" | sed 's/ *Fetch URL: //' | sed 's/https:\/\/github.com\//[email protected]:/')Chondrite
will it work for multiple github or bitbucket repostories and multiple accounts, will they conflict with each other?Asthenic
S
34

In case you are indeed using the SSH URL, but still are asked for username and password when git pushing:

git remote set-url origin [email protected]:<Username>/<Project>.git

You should try troubleshooting with:

ssh -vT [email protected]

Below is a piece of sample output:

...
debug1: Trying private key: /c/Users/Yuci/.ssh/id_rsa
debug1: Trying private key: /c/Users/Yuci/.ssh/id_dsa
debug1: Trying private key: /c/Users/Yuci/.ssh/id_ecdsa
debug1: Trying private key: /c/Users/Yuci/.ssh/id_ed25519
debug1: No more authentication methods to try.
Permission denied (publickey).

I actually have already added the public key to GitHub before, and I also have the private key locally. However, my private key is of a different name called /c/Users/Yuci/.ssh/github_rsa.

According to the sample output, Git is trying /c/Users/Yuci/.ssh/id_rsa, which I don't have. Therefore, I could simply copy github_rsa to id_rsa in the same directory.

cp /c/Users/Yuci/.ssh/github_rsa /c/Users/Yuci/.ssh/id_rsa

Now when I run ssh -vT [email protected] again, I have:

...
debug1: Trying private key: /c/Users/Yuci/.ssh/id_rsa
debug1: Authentication succeeded (publickey).
...
Hi <my username>! You've successfully authenticated, but GitHub does not provide shell access.
...

And now I can push to GitHub without being asked for username and password :-)

Scathing answered 26/1, 2018 at 18:15 Comment(2)
why would anyone want to authenticate in any other way, huh? love this, thanks!Indulgent
You might not have to copy the key to the other name; you could use something like ssh-add 'C:\Users\Yuci\.ssh\github_rsa' instead.Cephalopod
I
19

As usual, create an SSH key and paste the public key to GitHub. Add the private key to ssh-agent. (I assume this is what you have done.)

To check everything is correct, use ssh -T [email protected]

Next, don't forget to modify the remote point as follows:

git remote set-url origin [email protected]:username/your-repository.git
Isolate answered 24/12, 2018 at 15:9 Comment(0)
S
16

Additionally for gists, it seems you must leave out the username

git remote set-url origin [email protected]:<Project code>
Satisfactory answered 7/10, 2014 at 7:5 Comment(0)
R
10

You have to use the SSH version, not HTTPS. When you clone from a repository, copy the link with the SSH version, because SSH is easy to use and solves all problems with access. You can set the access for every SSH you input into your account (like push, pull, clone, etc...)

Here is a link, which says why we need SSH and how to use it: step by step

Git Generate SSH Keys

Reins answered 6/5, 2016 at 15:2 Comment(0)
M
5

Like the other users mentioned, you must convert it from using HTTPS to SSH. I don't see an answer with an end-to-end solution. After setting up the ssh keys, do (on your local machine) :

$ git remote set-url origin [email protected]:username/your_repo.git  # Convert HTTPS -> SSH
$ ssh-add ~/.ssh/id_rsa_github    # add private github ssh key ssh-agent (assuming you have it already running)
$ git push
Minor answered 30/11, 2021 at 12:22 Comment(0)
I
5

for using SSH you must use $ git remote add origin [email protected]:USERNAME/REPOSITORY.git instead of git remote add origin remote_repository_URL. you can check it with $ git remote -v, if you see 2 lines in the below format, it will work correctly:

origin [email protected]:username/repo-name.git (fetch)
origin [email protected]:username/repo-name.git (push)
Ichnography answered 1/6, 2022 at 13:36 Comment(0)
D
2

You did everything ok but git still asking by password, this worked for me, execute the next commando in your current project's path:

~ ssh-add -K ~/.ssh/id_rsaYourIdRsa

Add your SSH private key to the ssh-agent and store your passphrase in the keychain. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_rsaYourIdRsa in the command with the name of your private key file.

Dugald answered 19/11, 2021 at 7:40 Comment(0)
N
-5

Using the command line:

Enter ls -al ~/.ssh to see if existing SSH keys are present.

In the terminal is shows: No directory exist

Then generate a new SSH key

Step 1.

ssh-keygen -t rsa -b 4096 -C "[email protected]"

step 2.

Enter a file in which to save the key (/Users/you/.ssh/id_rsa): <here is file name and enter the key>

step 3.

Enter passphrase (empty for no passphrase): [Type a password]

Enter same passphrase again: [Type password again]
Needlework answered 3/3, 2017 at 20:23 Comment(1)
Sorry, this question is not about how to create the keys but to setup git to use the key instead of authenticationTieratierce

© 2022 - 2024 — McMap. All rights reserved.