Setup SSH in GitHub
- This works for MCC or any secure shell (SSH), such as the Gemini system
- The goal of this tutorial is to show how to use GitHub on a supercomputer and how to authenticate your GitHub account without a password
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.
- On GitHub, navigate to username > Settings > Developer settings > Personal access tokens > Tokens (classic) > Generate new token
- Press New personal access token (classic)
- Edit the note to reflect a descriptive title for what this is used for. For instance āgithub-user-loginā
- Set an expiration date. This can be set to up to a year in the future.
- 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
- Press Generate Token
- 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
- 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
- Login to mcc or other secure shell.
ssh user@mcc.uky.edu
# Enter password:
- Go to home directory and then to .ssh hidden file
cd
cd ~/.ssh
- 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"
- The output will be cool ASCII art and the public key. The -o flag forces the tools to form key pair in OpenSSH format. -t flag specifies rsa type, and -C specifies adding metadata (your GitHub email) to the end of the public key
Connect to GitHub with SSH
- 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
- 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
- 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
- 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
- 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:
- 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
- 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:
- Make a project repository on GitHub. Go to username > Repositories > New
- Clone the repository to your machine into a folder of the same name as the repository.
- 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
- 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.
- Make a text file (e.g. a program) on your local machine
- git add filename to stage the file. git add -A adds all of the files to the queue.
- git commit -m ācommit messageā. Commits the file to GitHub.
- git push. Pushes changes to GitHub. This syncs your progress. git push -all origin to push all your changes.
- 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
- This is how a workflow with generally go after git clone, git init and adding a README.md file
# 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
- 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
- ā# Project Titleā >> README.md. Make a README and add the project title
git init
- Make a new branch
Git checkout -B newbranch name
- Git status to check what file changes you have made and what branch you are on
git status
- Make new text files with vim, nano or text editor of your choice.
- 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
- 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"
- Git push. Sync all changes with github.
git push # push most recent file
git push -all # push all files
- Add the branch to GitHub
git push --set-upstream origin newbranchname
- 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
- Merge the branch to the main branch.
git merge newbranchname
- Get off the branch with git checkout
git checkout <destination> # most likely main
Other commands
- git log. Shows you a log of all file changes
- git diff file1 file2. Shows differences between file1 and file2 or branches
- git fetch. Fetch branches and/or tags (collectively, "refs") from one or more other repositories, along with the objects necessary to complete their histories.
- git config. Changes global git settings
- git pull <remote> is the same as git fetch <remote> followed by git merge origin/<current branch>
References
https://stackoverflow.com/questions/68775869/message-support-for-password-authentication-was-removed