Adding a SSH Key to Github

                             Git
"Entering Username & password every time you push to git, kind of pain right ?"

 1.Check for existing ssh keys.

   ssh keys are located in your home directory in a sub directory call .ssh

   Enter to see if existing SSH keys are available

ls -al ~/.ssh

 2. Check the directory listing to see if you already have a public SSH key.
  •   id_rsa.pub
  •   id_rsa
3. If you don't have an existing public and private key pair, Then you need to generate new SSH key.

Generating  a new SSH key

  • Open your terminal or git bash in windows, Enter
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" 

# Creates a new ssh key, using the provided email as a label Generating public/private rsa key pair.
  • Press Enter to accept default file location
  • Then you will be prompt enter passphrase, keep it  default to empty and press enter and enter again to confirm it.
  • Now your ssh keys are created
  • copy the content of the id_rsa.pub
  • These are the steps provided by Github.

Adding a new SSH key to your GitHub account

  • Now it's almost done
  • In GitHub site, Account Setting>SSH and GPG keys>New SSH Key. Paste the key we copied. Then, click "Add key".
  • These are the steps if you miss anything.

Checking your ssh key

we can check our ssh working properly
ssh -T git@github.com
check ssh
  • Now you have successfully set up your ssh key. one last thing !!!

You need to Switch remote URLs from HTTPS to SSH (for existing repository)

  • Open Terminal.
  • Change the current working directory to your local project.
  • List your existing remotes in order to get the name of the remote you want to change

  • git remote -v
    origin  https://github.com/USERNAME/REPOSITORY.git (fetch)
    origin  https://github.com/USERNAME/REPOSITORY.git (push)
    

  • Click clone or download button in your repository, select Use SSH and copy the URL.
Ssh
  • Change your remote's URL from HTTPS to SSH with the git remote set-url command.
git remote set-url origin git@github.com:USERNAME/OTHERREPOSITORY.git
  • Verify that the remote URL has changed.
git remote -v
origin  git@github.com:USERNAME/OTHERREPOSITORY.git (fetch)
origin  git@github.com:USERNAME/OTHERREPOSITORY.git (push)

Now you don't need to enter your credentials  every time when you git push !!!

That's it , remember for a new project or existing if you need to use the ssh key set your remotes to SSH

Thank you for reading :)