Git and SSH 👷🏼♂️👷🏼♀️
Stage 1. Get a key
Check for existing keys first:-
ls -la ~/.ssh
If no suitable keys exist, generate a new one:-
ssh-keygen -o -b 4096 -t rsa
(-o uses the newer OpenSSH format rather than the PEM format)
ssh-add <id_file_name>
(might need to start the ssh agent first with eval $(ssh_agent -s)
)
Now you have a suitable SSH key pair.
Stage 2. Add public key to the git service
Copy contents of <id_file_name>.pub
to clipboard. Paste this into the relevant section of the whichever git service you are using. When adding public keys it might make sense to label them with which device is using that key to access the service (e.g. 'work-mac', 'my win laptop')
GitHub
The place to enter the public ssh key can be found in: Account/ Settings/SSH & GPG keys
BitBucket
The place to enter the public ssh key can be found in: Account icon (bottom left of page) -> bitbucket settings -> security -> ssh keys
Stage 3. Clone the repo
Now can clone the repo with the ssh URL rather than the https URL
In github this looks like: git clone git@github.com:myusername/reponame.git
In bitbucket this looks like: git clone ssh://git@bitbucket.org/teamname/reponame.git
If updating an existing repo the remote URL can be updated with: git remote set-url origin [ssh version of url]
Handy ssh commands
ssh-add -l
lists loaded ssh keys
ssh-add -K [path/to/private SSH key
(mac) possibly adds id to Keychain... need to check this out
ssh-add -A
add all ssh-keys