Git SSH
Enabling SSH Access to GitHub
Here I want to put down the steps I did to enable ssh access from my machine to github so that I don’t have to login everytime.
1. Install OpenSSH
- First make sure to have openssh installed. As I was on a fresh arch linux, openssh was not installed by default. So I had to download it first.
2. Generate SSH Key Pair
- Then I had to generate a key pair. I used the below command to generate the keys:
ssh-keygen -t ed25519 -C "your_email@example.com"
- I then provided a new name to key when it prompted me for the file to which this key will be saved.
- You can provide a passphrase, but I didn’t.
3. Start SSH Agent and Add Key
- Then I used the below command to start the ssh-agent:
eval "$(ssh-agent -s)"
- Once that is done, we add the private key to the ssh-agent using the below command:
ssh-add ~/.ssh/id_ed25519
4. Add SSH Public Key to GitHub Account
- Then we add the ssh public key to the github account.
- I am using the github on the browser and will be using the key which I generated for both authentication and signing of commits.
- So first we copy the public key which we generated.
- Go to account settings in github.
- Access -> SSH and GPG keys -> New SSH Key
- For the title, I just used
github_key
and pasted my public key in the key field. - I did this process twice, once by putting key type to
authentication
and then tosigning
. - Then just click
ADD SSH key
and confirm by putting the password or MFA code.
5. Test SSH Connection
- Once all this was completed I tested the connection by using the below command:
ssh -T git@github.com
- Received a warning when testing for the first time like below:
> The authenticity of host 'github.com (IP ADDRESS)' can't be established.
> ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
> Are you sure you want to continue connecting (yes/no)?
- Hit enter and we have the connection established.
Configure Git for Commit Signing
Now to configure git to make sure to sign all my commits, so that all our commits are shown as verified on github.
1. Configure GPG Format
- We first tell git what key we will be using. So we first configure git to use ssh key which we generated:
git config --global gpg.format ssh
2. Set Signing Key
- Then we set the signing key which git will use to sign the commits using below command:
git config --global user.signingkey /PATH/TO/.SSH/KEY.PUB
3. Enable Automatic Commit Signing
- Now to make sure that all our commits are signed I used the below command:
git config --global commit.gpgsign true
- Now using just:
git push
- Automatically pushed the local changes to my github without providing the authentication.
Additional Notes
- I had to change the git origin from
https
tossh
using a git command (e.g.,git remote set-url origin git@github.com:user/repo.git
). - Also to manually sign commits use the below command which is not needed in my case:
git commit -S -m "YOUR_COMMIT_MESSAGE"