Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save programmerShinobi/cefde63fe30ed63f10956e04be7564ac to your computer and use it in GitHub Desktop.
Save programmerShinobi/cefde63fe30ed63f10956e04be7564ac to your computer and use it in GitHub Desktop.
Git Push to GitHub Using SSH

Git Push to GitHub Using SSH

Introduction

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.

Prerequisites

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

Steps to Git Push Using SSH

1. Generate an SSH Key (if you don’t have one already)

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.

2. Add the SSH Key to SSH Agent

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

3. Add Your SSH Key to GitHub

Next, you need to add the SSH public key to your GitHub account.

  1. Copy your public key to your clipboard:

    $ cat ~/.ssh/id_ed25519.pub

    Then, copy the output.

  2. Go to GitHub SSH Settings.

  3. Click on New SSH key, paste the copied key into the field, and save it.

4. Set the GitHub Remote URL to Use SSH

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.

5. Push Code to GitHub

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.

6. Verify SSH Authentication

To verify that the SSH connection to GitHub is working correctly, run:

If successful, you will see a message saying:

Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.

Conclusion

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment