Pushing Git Changes without Typing Passwords
I’m lazy and use SSH public keys with my GitHub/GitLab repositories so I don’t have to type a long password every time I push to one of my repos. This is a poor security practice, but let’s document how it works anyway!
The short version: you upload a public key to your account settings on GitHub/GitLab. After that, these services will authenticate using that public key to check that you can prove you have the corresponding private key. If you do, then your git commands will run…no passwords typed!
The tricky part is determining why your SSH operations fail to work. It boils down to specifying WHICH KEYS ARE OFFERED and knowing how to SPECIFY THE CORRECT HOST URL, which is not as straightforward as you would think. There are two main concepts behind making it work:
-
The git
remoteis an alias for the URL of the repository you are tracking. This is set using thegit remote add <alias> <url>command. For SSH the URL is the same format as a SSH login (e.g.git@gitlab.com:/path/to/repo/reponame.git).Note that the usernane should begitfor both GitLab and GitHub, not your login name KEY: Thegitlab.comin the connection string can refer to aHostentry in SSH config. -
Your public/private SSH key pairs (aka “identities”) are typically stored in your user directory at
~/.sshThe files with.pubextensions are your public keys, the ones without any extension are private keys. Don’t share your private key! Don’t forget to back them up when reinstalling the operating system! A program calledssh-agentis loaded to “offer” a list of identities to the remote host, which will then try them one-after-the-other until it gets a response that PROVES you are indeed a valid user of the repository you’re accessing. KEY: TheHostentry in SSH config can be be told to offer a different identities even to the same domain (e.g.github.com)
1. Choosing what identity keys are offered
The available key pairs (“identities”) are stored in ~/.ssh and referred to in the ~/.ssh/config settings file. However, it is the helper program ssh-agent that does the actual communication with a remote server to authenticate you before your git commands are allowed to run. This helper program usually runs automatically on computer startup.
When using the command line, ssh-agent is initialized with a set of keys using the ssh-add ~/.ssh/keyname command. This temporarily adds the identities you want to authenticate with. You also use this command to remove all identities with ssh-add -D to start fresh. Finally, you can use ssh-add -l to list all the currently loaded identities. To connect to different remote hosts with SSH, you use these commands to load/unload the proper identities for each.
Remember, though, that I’m choosing convenience over security. There are several shortcuts you can add by adding Host entries to the ~/.ssh/config file.
2. Setting the correct Host options
The Host entries in ~/.ssh./config can be used as an address in an SSH connection string. This is super important, because it allows you to specify a particular IdentityFile to go with a particular HostName. Consider these two examples defined in the same config file:
# address one
Host github.com
HostName github.com
User git
PreferredAuthentications publickey
IdentityFile /Users/dsri/.ssh/id_rsa
# address two
Host github.com-extra
HostName github.com
User git
PreferredAuthentications publickey
IdentityFile /Users/dsri/.ssh/github-ed25519
Note: At the time of this writing, the SourceTree git client adds its own entries to ~/.ssh/config so you should review them, particularly when setting a git remotes for the first time with a new repo.