šŸ”

Setup SSH in GitHub

Create Personal Access Token on GitHub

A personal access token is used in lieu of a password when cloning private repositories to your local machine or a server that runs with SSH.

  1. On GitHub, navigate to username > Settings > Developer settings > Personal access tokens > Tokens (classic) > Generate new token
  1. Press New personal access token (classic)
  1. Edit the note to reflect a descriptive title for what this is used for. For instance ā€œgithub-user-loginā€
  1. Set an expiration date. This can be set to up to a year in the future.
  1. Select scopes (permissions) for what you can access with the personal access token. To be conservative select repo, write:packages, delete:packages, admin:public_key, read:user, write:discussion, project, and admin:ssh_signing_key

  1. Press Generate Token
  1. Copy the token now (you will only be able to see it once) and store it in a safe location. Important: copy the personal access token now and save it in a secure location
  1. On the mcc, change your git config settings to match your GitHub settings. Check these changes with git config -l
git config --global user.name "your_github_username"
    git config --global user.email "your_github_email"
    git config -l

Create GitHub SSH Keys

  1. Login to mcc or other secure shell.
ssh user@mcc.uky.edu
    # Enter password:

  1. Go to home directory and then to .ssh hidden file
cd
    cd ~/.ssh

  1. Use the ssh-keygen command to create a GitHub SSH key pair. Press return to leave key in .ssh folder. Press return again to not make a passphrase (unless you want extra security)
ssh-keygen -o -t rsa -C "githubemail@example.com"

Connect to GitHub with SSH

  1. View the contents of the public key. Copy them to your clipboard (this is an example donā€™t copy the one in the box below).
cat id_rsa.pub
    ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCwrUzqtm
    3K9YNI2WbXxkcfnHZgyW7/3WXghBbKndhbKbCR00JLfTHsK
    Kaz17c4xIHQrw7u0GsPXai6pMtwMeVmXQH00L5hD0WE5Ioo 
    =githubemail@example.com

  1. GitHub SSH key configuration. Configure your GitHub SSH key in the settings of your online GitHub account. Navigate to username > Settings > SSH and GPG Keys > Add New

  1. Title the ssh-key how you please. Make it descriptive like: servername-key. Paste the public key into the box and press Add SSH key
  1. Navigate to your repository on GitHub or create a new one. When cloning a repository to the mcc, or other secure shell, press Code. Then copy the SSH link
  1. On the mcc, type (fill in the details with your username and repository name). Enter your GitHub username and the personal access token generated in the first part. Password authentication is no longer supported as of August 2021.
git clone git@github.com:username/repository-name 
    Username: 
    Password:

  1. You have now successfully cloned your repository. Anytime you want to clone a private repository, you should enter your personal access token not your password. OR
  1. Save your personal access token on the disk indefinitely (warning: not very secure)
git config credential.helper store
    git push http://example.com/repo.git
    Username: <type your username>
    Password: <type your personal access token>
    
    [several days later]
    git push http://example.com/repo.git
    [your credentials are used automatically]

https://git-scm.com/docs/git-credential-store

Basic Git Commands

The basic workflow in git is to:

  1. Make a project repository on GitHub. Go to username > Repositories > New
  1. Clone the repository to your machine into a folder of the same name as the repository.
  1. Add README.md file and .git file (this can happen on step 1 in GitHub by checking a box). ā€œ# Titleā€ >> README.md then run git init
  1. Make a branch (unless this is a personal project, you donā€™t want to mess with the main branch). git checkout -B newbranchname. Necessary if you are working on a collaborative project. If you own the repository, donā€™t do this.
  1. Make a text file (e.g. a program) on your local machine
  1. git add filename to stage the file. git add -A adds all of the files to the queue.
  1. git commit -m ā€œcommit messageā€. Commits the file to GitHub.
  1. git push. Pushes changes to GitHub. This syncs your progress. git push -all origin to push all your changes.
  1. Iterate until you have a great project. If someone else made changes you should run git pull <remote> before working, which remote being the HTTPS or SSH link of the repository.

Workflow with Fast Forward Merge

# Start a new feature
    git checkout -b new-feature main
    # Edit some files
    git add <file>
    git commit -m "Start a feature"
    # Edit some files
    git add <file>
    git commit -m "Finish a feature"
    # Merge in the new-feature branch
    git checkout main
    git merge new-feature
    git branch -d new-feature

Git commands

  1. Git clone. Used to clone a remote repository to your machine
git clone git@github.com:username/repository-name # SSH
    git clone https://github.com/username/repository-name.git #HTTPS
  1. ā€œ# Project Titleā€ >> README.md. Make a README and add the project title
git init
  1. Make a new branch
Git checkout -B newbranch name
  1. Git status to check what file changes you have made and what branch you are on
git status
  1. Make new text files with vim, nano or text editor of your choice.
  1. Git add filename. Add a file to stage it for syncing with GitHub. Do this for every new file or just do git add -A
git add filename # add one file
    git add -A # add all files
  1. Git commit -m ā€œcommit messageā€. This commits the files to GitHub with a message. The message is required. Make the message descriptive.
git commit -m "commit message"
  1. Git push. Sync all changes with github.
git push # push most recent file
    git push -all # push all files
  1. Add the branch to GitHub
git push --set-upstream origin newbranchname
  1. Make a pull request so the owner can approve your code to be added to main. If you are the owner of the GitHub repository, no need to do this. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request
  1. Merge the branch to the main branch.
git merge newbranchname
  1. Get off the branch with git checkout
git checkout <destination> # most likely main

Other commands

References

https://stackoverflow.com/questions/68775869/message-support-for-password-authentication-was-removed

https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/GitHub-SSH-Key-Setup-Config-Ubuntu-Linux

https://git-scm.com/docs