Press "Enter" to skip to content

Connecting to GitHub Step by Step

Connect Your Environment to GitHub
It is that time of the day to overcome the dread of Git. So can we please ask the all important question right now?

What is Git?
According to Wikipedia;

Git is software for tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development. Its goals include speed, data integrity, and support for distributed, non-linear workflows.

A successful connection to GitHub does require an established trust between the client {your working host} and the server {GitHub}.

An SSH key relies upon the use of two related keys, a public key and a private key, that together create a key pair that is used as the secure access credential. The private key is secret, known only to the user, and should be encrypted and stored safely. The public key can be shared freely with any SSH server to which the user wishes to connect.

Public Key authentication – what and why?

The motivation for using public key authentication over simple passwords is security. Public key authentication provides cryptographic strength that even extremely long passwords can not offer. With SSH, public key authentication improves security considerably as it frees the users from remembering complicated passwords (or worse yet, writing them down).

In addition to security public key authentication also offers usability benefits – it allows users to implement single sign-on across the SSH servers they connect to. Public key authentication also allows automated, passwordless login that is a key enabler for the countless secure automation processes that execute within enterprise networks globally.

Checking for existing SSH keys
Before you generate an SSH key, you can check to see if you have any existing SSH keys.

  • Open Git Bash.
  • Enter ls -al ~/.ssh to see if existing SSH keys are present.
  • ls -al ~/.ssh
    total 27
    drwxr-xr-x 1 Cloud Architect 197121    0 May 16 09:39 ./
    drwxr-xr-x 1 Cloud Architect 197121    0 May 10 14:40 ../
    -rw-r--r-- 1 Cloud Architect 197121  419 May 16 09:39 githubaccount
    -rw-r--r-- 1 Cloud Architect 197121  107 May 16 09:39 githubaccount.pub
    -rw-r--r-- 1 Cloud Architect 197121 2675 Nov 11  2021 id_rsa
    -rw-r--r-- 1 Cloud Architect 197121  585 Nov 11  2021 id_rsa.pub
    -rw-r--r-- 1 Cloud Architect 197121 1422 Mar 18 16:50 known_hosts
    

    The known_hosts file contains details of your remote hosts and their public keys.

    Generating a new SSH key

    $ ssh-keygen -t ed25519 -C "your_email@domain.com"

    This creates a new SSH key, using the provided email as a label.

    > Generating public/private algorithm key pair.
    

    When you’re prompted to “Enter a file in which to save the key,” press Enter. This accepts the default file location.

    > Enter a file in which to save the key (/c/Users/you/.ssh/id_algorithm):[Press enter]
    
    Generating public/private ed25519 key pair.
    Enter file in which to save the key (/c/userPath/.ssh/id_ed25519): /c/userPath/.ssh/githubaccount
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /c/userPath/.ssh/githubaccount
    Your public key has been saved in /c/userPath/.ssh/githubaccount.pub
    The key fingerprint is:
    SHA256:CZaWiynl94Vk6dkqXXIBCnewmbgY6vhGnqeq8PrRmulQ git.user@domain.com
    The key's randomart image is:
    +--[ED25519 256]--+
    |    .o..o+. o.   |
    |. o .oo.+o o..   |
    |oo +. .oo....o.  |
    |=o..  .  + .o..  |
    |o.=E   .S..o..   |
    | *.     . + o    |
    |o.+        o     |
    |+*               |
    |@*o              |
    +----[SHA256]-----+
    
    Cloud Architect@DESKTOP-ATCRUJV MINGW64 /c/Workdir/terraform
    $
    
    

    At the prompt, type a secure passphrase. For more information, see “Working with SSH key passphrases.”

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

    Copy the SSH public key to your clipboard.

    If your SSH public key file has a different name than the example code, modify the filename to match your current setup. When copying your key, don’t add any newlines or whitespace.

    Adding your SSH key to the ssh-agent
    Before adding a new SSH key to the ssh-agent to manage your keys, you should have checked for existing SSH keys and generated a new SSH key.
    If you have GitHub Desktop installed, you can use it to clone repositories and not deal with SSH keys.

    Ensure the ssh-agent is running. You can use the “Auto-launching the ssh-agent” instructions in “Working with SSH key passphrases”, or start it manually:

    # start the ssh-agent in the background
    $ eval "$(ssh-agent -s)"
    Agent pid 1304
    
    $ clip < ~/.ssh/githubaccount.pub
    # Copies the contents of the id_ed25519.pub file to your clipboard
    

    There are no SSH keys associated with your account

    In the "Access" section of the sidebar, click SSH and GPG keys.
    Click New SSH key or Add SSH key.

    add public ssh key to github
    add public ssh key to github

    added new public ssh key to github successfully
    added new public ssh key to github successfully

    create new repository on github
    create new repository on github

    Create new Public Repository on GitHub
    create new public repository on github

    Create new Public Repository on GitHub
    created a new public repository on github

    Testing your SSH connection
    After you've set up your SSH key and added it to your account on GitHub.com, you can test your connection.

    Enter the following:

    $ ssh -T git@github.com
    # Attempts to ssh to GitHub
    

    You may see a warning like this:

    > The authenticity of host 'github.com (IP ADDRESS)' can't be established.
    > RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
    > Are you sure you want to continue connecting (yes/no)?

    Troubleshooting

    $ ssh -vT git@github.com:remote-git-repo/azterraform.git
    OpenSSH_8.8p1, OpenSSL 1.1.1l  24 Aug 2021
    debug1: Reading configuration data /etc/ssh/ssh_config
    debug1: resolve_canonicalize: hostname github.com:remote-git-repo/azterraform.git is an unrecognised address
    ssh: Could not resolve hostname github.com:remote-git-repo/azterraform.git: Name or service not known
    

    Solutions are always in the problems

    $ ssh -vT git@github.com:remote-git-repo/azterraform.git
    OpenSSH_8.8p1, OpenSSL 1.1.1l  24 Aug 2021
    debug1: Reading configuration data /c/userPath/.ssh/config
    debug1: Reading configuration data /etc/ssh/ssh_config
    debug1: resolve_canonicalize: hostname github.com:remote-git-repo/azterraform.git is an unrecognised address
    ssh: Could not resolve hostname github.com:remote-git-repo/azterraform.git: Name or service not known
    

    Add the following line to /etc/ssh/ssh_config file.

    add Identity File to ssh_config file to tell ssh where to find your private key

    # Host *
    #   StrictHostKeyChecking ask
    #   IdentityFile ~/.ssh/id_rsa
    #   IdentityFile ~/.ssh/id_dsa
    #   IdentityFile ~/.ssh/id_ecdsa
    #   IdentityFile ~/.ssh/id_ed25519
        IdentityFile ~/.ssh/githubaccount
    #   Port 22
    #   Ciphers aes128-ctr,aes192-ctr,aes256-ctr,aes128-cbc,3des-cbc
    #   MACs hmac-md5,hmac-sha1,umac-64@openssh.com
    

    Create the config file under ~/.ssh/ and add the following lines

    Setup Your Local Working Environment

    Clone your remote Repository - use git clone

    $ git clone git@github.com:remote-git-repo/azterraform.git
    Cloning into 'azterraform'...
    remote: Enumerating objects: 6, done.
    remote: Counting objects: 100% (6/6), done.
    remote: Compressing objects: 100% (2/2), done.
    Receiving objects: 100% (6/6), done.
    remote: Total 6 (delta 0), reused 0 (delta 0), pack-reused 0
    Cloud Architect@DESKTOP-ATCRUJV MINGW64 /c/Workdir/terraform (master)
    $
    

    Now begin to perform your functions

    touch filename.tf
    

    Add all items in your working directory using git add .

    $ git add .
    

    Commit your changes to your repo.

    $ git commit -m "Version 1 - Files Created on Local PC"
    [main 2f462f4] Version 1 - Files Created on Local PC
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 main.tf
    

    Remove Git from Folder

    rm -rf .git