> For the complete documentation index, see [llms.txt](https://dshub.gitbook.io/ds-hub/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dshub.gitbook.io/ds-hub/git/connecting-to-github.md).

# 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:

1. We first need to generate an SSH key on our local machines.&#x20;
2. 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

1. Open Terminal.
2. Paste the text below, replacing the email address used on your GitHub account .

   ```shell
   ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
   ```

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.

```shell
> Enter a file in which to save the key (/Users/YOU/.ssh/id_ALGORITHM): [Press enter]
```

3. At the prompt, type a secure passphrase. For more information, see "[Working with SSH key passphrases](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/working-with-ssh-key-passphrases)."

   ```shell
   Enter passphrase (empty for no passphrase): [Type a passphrase]
   > Enter same passphrase again: [Type passphrase again]
   ```

## Adding your SSH key to the ssh-agent <a href="#adding-your-ssh-key-to-the-ssh-agent" id="adding-your-ssh-key-to-the-ssh-agent"></a>

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.

1. Start the ssh-agent in the background.

   ```shell
   $ eval 'ssh-agent'
   > Agent pid 59566
   ```

Depending on your environment, you may need to use a different command. For example,&#x20;

* you may need to use root access by running `sudo -s -H` before starting the ssh-agent,&#x20;
* or you may need to use `exec ssh-agent bash` or `exec ssh-agent zsh` to run the ssh-agent.

2. 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.

  ```shell
  $ open ~/.ssh/config
  > The file /Users/YOU/.ssh/config does not exist.
  ```
* If the file doesn't exist, create the file.

  ```shell
  touch ~/.ssh/config
  ```
* Open your `~/.ssh/config` file using `vim` 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.

  ```
  Host github.com
    AddKeysToAgent yes
    UseKeychain yes
    IdentityFile ~/.ssh/<private_key_file>
  ```

{% hint style="info" %}
**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.

  ```
  Host github.com
    IgnoreUnknown UseKeychain
  ```

{% endhint %}

3. Add your SSH private key to the ssh-agent and store your passphrase in the keychain with the name of your private key file.&#x20;

   ```
   ssh-add -K ~/.ssh/<private_key_file> 
   ```

{% hint style="info" %}
In macOS versions prior to Monterey (12.0),&#x20;

* -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).
{% endhint %}

4. Copy the SSH public key to your clipboard.&#x20;

```shell
$ pbcopy < ~/.ssh/<public_key_file>.pub
# Copies the contents of the <public_key_file>.pub file to your clipboard
```

## Add the SSH public key to your GitHub account

1. Go to Profile>Settings>SSH and GPG keys.&#x20;
2. Click **New SSH key** or **Add SSH key** on the upper right corner.
3. 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".

<figure><img src="https://483934582-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F332OmkaCCBc9TZFZXfnO%2Fuploads%2F7fFdyymnGvONuJgg7FR8%2FScreen%20Shot%202024-03-29%20at%202.43.36%20PM.png?alt=media&amp;token=0b9facc1-2442-4260-a00c-1092fd339246" alt=""><figcaption></figcaption></figure>

4. Select the type of key, either authentication or signing. For more information about commit signing, see "[About commit signature verification](https://docs.github.com/en/authentication/managing-commit-signature-verification/about-commit-signature-verification)."
5. In the "Key" field, paste your public key.
6. Click **Add SSH key**.
7. If prompted, confirm access to your account on GitHub. For more information, see "[Sudo mode](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/sudo-mode)."
