Tyler
Tyler

Reputation: 19858

How to configure command line git to use ssh key

When I do git pull via the command line, it always asks for my github username and password. I'd like to tell it to use the ssh key in github, and never have to worry about it again. How do you do this?

Upvotes: 54

Views: 156592

Answers (7)

mwsundberg
mwsundberg

Reputation: 330

Here's a consolidating answer if you want to use the same SSH key for signing commits as well as authenticating for clones/pulls/etc.

Given an SSH key-pair stored in .ssh/git_ed25519 and .ssh/git_ed25519.pub, this config will use that key for both signing commits and authenticating via SSH for remote repo access:

# Specify SSH key for remote authentication
[core]
      sshCommand = ssh -i ~/.ssh/git_ed25519

# Specify SSH key for commit signing
[user]
      signingKey = ~/.ssh/git_ed25519.pub
[gpg]
      format = ssh
[commit]
      gpgsign = true

Commands to add these options to the global config:

# SSH for remote authentication
git config --global core.sshCommand 'ssh -i ~/.ssh/git_ed25519'

# SSH for commit signing
git config --global user.signingKey '~/.ssh/git_ed25519.pub'
git config --global gpg.format ssh
git config --global commit.gpgsign true

Note that signing commits with SSH keys is only supported in Git v2.34 and up.


Citations:

  1. Rich Signell's answer for the core.sshCommand option
  2. Github docs for SSH commit signing options
  3. Git release notes for versions supporting ssh key commit signing

Upvotes: 1

venimus
venimus

Reputation: 6067

Git does not provide a dedicated way to configure which key to use for ssh connections (just uses whatever is set as default), but fortunately we can override the interal ssh command and specify it (add -i key_file)

1. To clone a repo with specific key you can use:

git clone -c "core.sshCommand=ssh -i ~/.ssh/id_rsa_repo" git@github.com:org/repo.git

Config will persist.

2. Then to change that to local config:

git config core.sshCommand "ssh -i ~/.ssh/id_rsa_repo"

Upvotes: 4

Rich Signell
Rich Signell

Reputation: 16445

Having looked at these answers, it seems the one I found easiest is not included. In the working directory of the repo you are interested in specifying a specific key, do:

git config core.sshCommand 'ssh -i ~/.ssh/id_rsa'  #specific private key

You can check to see that it worked (or see if there is an existing core.sshCommand for the repo) by doing:

git config --list

Upvotes: 49

Green 绿色
Green 绿色

Reputation: 2956

If you use a Gnome-based Linux desktop, such as Ubuntu or Fedora, you can also use the built-in "Passwords and encryption" application to create and manage SSH keys. To create an SSH key, just open the application, click +, then SSH Key, fill out the form, enter the password for the key twice and finally insert the generated public key information into GitHub. After that, the git command will use this SSH key for SSH connections. You have to enter the password for the SSH key only once per session. In this case there is no need to configure ssh-agent or modify ~/.ssh/config, it just works.

Upvotes: 1

Digital Divvy
Digital Divvy

Reputation: 11

Another possibility is that after creating the ssh you may have moved the keys to a different folder. In that case, ssh-add ~/yourkeyfolder/yourkey

This will ensure that your key is known to the os.

Upvotes: 1

Jack M
Jack M

Reputation: 6025

To tell Git to use the key that you generated, add the following to your ssh config (on Linux, usually located at ~/.ssh/config):

Host github.com
  User git
  IdentityFile ~/.ssh/id_rsa

For the IdentityFile you should use the key that was generated by ssh-keygen (not the one whose name ends in .pub). The User must always be git.

Upvotes: 62

merlin2011
merlin2011

Reputation: 75629

Assuming that you have used ssh-keygen to generate a key pair and uploaded the public key in the appropriate place in your github account, you should be able to set remote to use the url git@github.com:username/repo.git.

git remote set-url origin git@github.com:username/repo.git

If you do not have local changes that you care about, you can just delete your local repository and clone again:

git clone git@github.com:username/repo.git

Here are github's instructions on this setup, which you can use as a reference as needed.

Upvotes: 21

Related Questions