Connecting to GitHub
We can connect to git repositories in two ways: HTTPS and SSH.
HTTPS: asks for an access token every time we do a push to a repository.
SSH: enables us to use Git commands without requiring username and token.
SSH is the preferred method to communicate with GitHub in the industry and fairly simple to setup:
We first need to generate an SSH key on our local machines.
After generating the key, we add our public key to our account on GitHub.com
Once we complete the step 2, we will then be authenticated for Git operations over SSH.
Generating an SSH key on Mac
Open Terminal.
Paste the text below, replacing the email address used on your GitHub account .
This creates a new SSH key, using the provided email as a label. When you're prompted to "Enter a file in which to save the key", you can press Enter to accept the default file location.
At the prompt, type a secure passphrase. For more information, see "Working with SSH key passphrases."
Adding your SSH key to the ssh-agent
If we don't want to type our passphrase each time we use the key, we will need to add it to the ssh-agent.
Start the ssh-agent in the background.
Depending on your environment, you may need to use a different command. For example,
you may need to use root access by running
sudo -s -H
before starting the ssh-agent,or you may need to use
exec ssh-agent bash
orexec ssh-agent zsh
to run the ssh-agent.
If you're using macOS Sierra 10.12.2 or later, you will need to modify your
~/.ssh/config
file to automatically load keys into the ssh-agent and store passphrases in your keychain.
First, check to see if your
~/.ssh/config
file exists in the default location.If the file doesn't exist, create the file.
Open your
~/.ssh/config
file usingvim
or the text editor of your preference, then modify the file to contain the following lines. If your SSH key file has a different path than the example code, modify the file path to match your current setup.
Notes:
If you chose not to add a passphrase to your key, you should omit the
UseKeychain
line.If you see a
Bad configuration option: usekeychain
error, add an additional line to the configuration's'Host *.github.com
section.
Add your SSH private key to the ssh-agent and store your passphrase in the keychain with the name of your private key file.
In macOS versions prior to Monterey (12.0),
-K flag represents --apple-use-keychain
-A flag represents --apple-load-keychain
If you continue to be prompted for your passphrase, you may need to add the command to your ~/.zshrc
file (or your ~/.bashrc
or file for bash).
Copy the SSH public key to your clipboard.
Add the SSH public key to your GitHub account
Go to Profile>Settings>SSH and GPG keys.
Click New SSH key or Add SSH key on the upper right corner.
In the "Title" field, add a descriptive label for the new key. For example, if you're using a personal laptop, you might call this key "Personal laptop".
Select the type of key, either authentication or signing. For more information about commit signing, see "About commit signature verification."
In the "Key" field, paste your public key.
Click Add SSH key.
If prompted, confirm access to your account on GitHub. For more information, see "Sudo mode."
Last updated