This tutorial will guide you through the process of setting up SSH authentication with GitHub and pushing your code to a GitHub repository using SSH.
- A GitHub account.
- Git installed on your local machine.
- SSH key already generated and added to GitHub.
- An existing repository on GitHub or you need to create one.
If you don’t have an SSH key generated yet, you need to create one. Open a terminal and run the following command:
$ ssh-keygen -t ed25519 -C "[email protected]"
This will generate an SSH key pair. After running the command, follow the instructions to save the key to the default directory (~/.ssh/id_ed25519
). You can also set a passphrase for additional security.
Once the SSH key is generated, you need to add it to the SSH agent. Run the following commands:
$ eval "$(ssh-agent -s)"
$ ssh-add ~/.ssh/id_ed25519
Next, you need to add the SSH public key to your GitHub account.
-
Copy your public key to your clipboard:
$ cat ~/.ssh/id_ed25519.pub
Then, copy the output.
-
Go to GitHub SSH Settings.
-
Click on New SSH key, paste the copied key into the field, and save it.
Ensure that your Git repository is using SSH for the remote URL. If you haven’t already, change the remote URL by running the following command:
$ git remote set-url origin [email protected]:<username>/<repository-name>.git
Replace <username>
with your GitHub username and <repository-name>
with the name of your repository.
Now, you are ready to push your local changes to the GitHub repository using SSH. To do so, run the following command:
$ git push -u origin main
This will push your code to the main branch of your GitHub repository.
To verify that the SSH connection to GitHub is working correctly, run:
$ ssh -T [email protected]
If successful, you will see a message saying:
Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.
Congratulations! You have successfully set up SSH authentication and pushed your code to GitHub using SSH. Now, you won’t need to enter your GitHub credentials each time you push changes.