Skip to content

Instantly share code, notes, and snippets.

@mortenege
Created August 24, 2018 04:57
Show Gist options
  • Save mortenege/42b12a82f7d79877171af84c7d0a0714 to your computer and use it in GitHub Desktop.
Save mortenege/42b12a82f7d79877171af84c7d0a0714 to your computer and use it in GitHub Desktop.
How to set up using GitHub SSH with keys without agent
1. This assumes an SSH keypair exists, usually in ~/.ssh (windows C:\Users\<username>\.ssh)
2. This assumes a git repository exists, otherwise create one as usual `git init`
3. Create a Repository on GitHub
4. Add SSH key to GitHub
5. Add the remote origin from gthub
```
git remote add origin git@github.com:<username>/<repo>.git
```
use `git remote -v` to verify
6. Edit your SSH config-file `~/.ssh/config` (windows `C:\Users\<username>\.ssh\config`) and add a Host
```
Host github
HostName github.com
User git
IdentityFile "path/to/ssh/private-key"
IdentitiesOnly yes
```
7. Edit the git projects config-file `.git\config` and add in [core]
```
sshCommand = "ssh -F <path/to/ssh/config/file>"
```
8. Use git push like usual
```
git push origin master
```
@sw2njw
Copy link

sw2njw commented Aug 19, 2022

This is very helpful!
I found it also works with just one line in .git/config: sshCommand = "ssh -i ~/.ssh/github_key"
Which does not require editing two files.

Thanks!

@Amauryeen
Copy link

This is very helpful! I found it also works with just one line in .git/config: sshCommand = "ssh -i ~/.ssh/github_key" Which does not require editing two files.

Thanks!

Shorter and works well, thank you!

@brokensandals
Copy link

This was helpful, thank you both! I'll add that the ssh command can also be set via the GIT_SSH_COMMAND environment variable if editing the config is inconvenient.

@jacobsvensmark
Copy link

jacobsvensmark commented Aug 29, 2023

Ill just add that for some reason this did't work and trying git push origin main (my branch is called main, not master) I was getting git@github.com: Permission denied (publickey)'.. However putting only the following in the ~/.ssh.config file:

Host github.com
 IdentityFile "path/to/ssh/private-key"

did work. So someone may want to try that if they are having the same issue as me. Maybe someone smart may immediately identify why the original .ssh/config was not working for me.

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