Securely connecting to GitHub using SSH is not only faster, but also more convenient and secure than using HTTPS. To achieve this, you'll need to set up an SSH key pair on your local machine and add the public key to your GitHub account. Let's guide you through the process with a practical example.

Before we dive into the details, ensure you have Git and OpenSSH installed on your local system. If not, you can install them using the package manager of your operating system. Now, let's generate your new SSH key pair.

Generating and Adding Your SSH Key
The first step is to generate a new SSH key pair on your local machine. This pair consists of a private key (which you keep secret) and a public key (which you share with others, like GitHub).

Here's how you can generate a new SSH key pair: 1. Open your terminal. 2. Run the following command to generate a new SSH key (replace "your-email@example.com" with your actual email):
ssh-keygen -t ed25519 -C "your-email@example.com"

The command will prompt you to choose a file location. Press Enter to accept the default location (~/.ssh/id_ed25519). You can also add a passphrase for extra security, but it's not mandatory for this example.
Adding Your SSH Key to SSH-Agent
After generating your key, you need to add it to your SSH Agent. This way, your key is loaded automatically when you open a new terminal session. 1. To add your key, run the following command:

eval "$(ssh-agent -s)"
This command starts the SSH Agent in the background and prints its command line. 2. Now, add your key to the agent:
ssh-add ~/.ssh/id_ed25519

The SSH Agent will ask for your passphrase if you added one during the key generation step.
Testing Your SSH Connection




















Before proceeding, let's ensure your SSH key is working correctly. Run the following command:
ssh -T git@github.com
If everything works fine, you should see a "Hi {Your Username}! You've successfully authenticated, but GitHub does not provide shell access." message.
Adding Your SSH Key to GitHub
Now that your SSH key is set up on your local machine, it's time to add it to your GitHub account. This enables GitHub to recognize you when you authenticate with your SSH key.
To add your key, follow these steps:
1. Navigate to https://github.com/settings/keys in your web browser. 2. Click "New Key." 3. Give your key a title, e.g., your machine's name or a description of the key's purpose. 4. In the "Key" field, paste the content of your public SSH key (found in the `~/.ssh` directory where you generated the key). To find this, open the key file (id_ed25519.pub) in a text editor and copy everything. 5. Click "Add Key" to save it.
Using Your SSH Key with Git
Finally, you need to tell Git to use your SSH key for SSH communication. This can be done in your `~/.gitconfig` file: 1. Open your `~/.gitconfig` file in a text editor. If the file doesn't exist, create it.
2. Add the following content to the file:
[user]
name = Your Name
email = your.email@example.com
signingkey = /path/to/your/id_ed25519
3. Save and close the file. Now, your SSH key is associated with your Git configurations.
Pushing Your First Repository
Now you can push your first repository to GitHub using SSH. Here's an example of how to do this:
1. Initialize a new Git repository: git init
2. Add your new files: git add .
3. Commit your changes: git commit -m "Initial commit"
4. Create a new repository on GitHub.
5. Connect your local repository to your new GitHub repository: git remote add origin git@github.com:yourusername/your-repo-name.git
6. Push your local repository to GitHub: git push -u origin master
And that's it! You've successfully added your SSH key to GitHub and pushed your first repository using SSH. As you continue to use Git and GitHub, using SSH will make your workflow faster and more secure.
Happy coding! Remember to keep your private SSH key safe and secret. Never share it with anyone. If you suspect your key has been compromised, revoke it from GitHub and generate a new one.