Push and Clone Github Repos with ed25519 SSH Authentication for Lazy Devs
Lets setup GitHub so we can authenticate with ssh keys
What is the best way to noob out on GitHub?
One way is to authenticate with a password when pushing your code to the cloud. There is another way that is way more lazy and more secure (which you are about to learn). Another way is to use git and GitHub interchangeably.
Git and GitHub are not the same thing.
By the end of this tutorial you will have mastered the art of pushing and pulling from GitHub using ssh authentication.
Prerequisites
- Git (Free) Installing Git (mac, windows, linux)
- Terminal or terminal emulator
- GitHub Account (Free) Signup GitHub.com
Signup for GitHub
Head over to github.com and setup an account.
The account can be setup for free. Email and username is required.
Your username will be part of your public profile.
Make sure the username you choose is aligned with the types of companies you want to work for.
For example 420NoobSlayer69
is memorable so that’s a plus. But is that what you want to link people to on your resume?
Setup GitHub.com noreply email settings
Would you like more control over what email is associated with every GitHub action you initiate (or initiated on your behalf by GitHub)?
Well if the answer is no, do not skip this step.
Head over to github.com/settings/account. Towards the bottom of the page you will see a checkbox for Keep my email addresses private.
- Enable the privacy setting.
- Copy the no reply email address to your clipboard.
The privacy setting does not change how your local system is setup.
Setup Global Git Config Locally
Ensure you have git installed on your local machine before proceeding.
When you work locally and make commits the logs will show metadata for each entry. This metadata includes your name and email.
Set your name globally
git config --global user.name "your name"
Set your email globally
git config --global user.email "your email"
The global settings are being persisted in your ~/.gitconfig
file. Try to use CLI commands to edit the config to prevent breaking it.
View the config using the cat
command.
cat ~/.gitconfig
Generate SSH Keypair
Let’s generate a new ed25519 SSH keypair.
For a dedicated learning resource on creating an ed25519 keypair Check out the Officail Lazy Article titled How to Generate an ed25519 Key Pair for SSH Authentication
For the purposes of this tutorial we will create a keypair without a password using the -N ''
flag.
It is your responsibility to assess the security implications of using ssh keys without passwords.
update the username in the command below
ssh-keygen -t ed25519 \
-C "github.com::<username>" \
-N '' \
-f ~/.ssh/gh_ed25519 \
-q
Esure correct private key permissions.
chmod 600 ~/.ssh/gh_ed25519
Update your local ssh config
Let’s setup your ssh config so juggling keys is no big deal.
Run the nano command on ~/.ssh/config
. If the file does not exits nano will create one on save. If nano is not your thing, you can use any text editor you like.
nano ~/.ssh/config
Let’s create an ssh config block for our authenticating with GitHub. This configuration tells SSH which key to use when connecting to GitHub.
Host github.com
HostName github.com
User git
IdentityFile /path/to/ssh-key
AddKeysToAgent yes
UseKeychain yes
UseKeychain
is MacOS specific.
If your ssh key does not have a password, do not include UseKechain
or AddKeysToAgent
config.
Add public ssh key to GitHub
Let’s get the public key on your clipboard.
Windows
clip < ~/.ssh/gh_ed25519.pub
MacOS
pbcopy < ~/.ssh/gh_ed25519.pub
Linux
cat ~/.ssh/gh_ed25519.pub
Head over to your account key settings page github.com/settings/keys
- Click the
New SSH key
button. - Paste the contents of the public key into the key field and add a title.
- Click the
Add SSH key
button.
Test your config
If all is setup correctly you will see an output with your GitHub username confirming the connection.
ssh -T git@github.com
Create a project directory
Use the terminal to create a new directory and change into that directory.
mkdir github-ssh && cd github-ssh
Create a README.md file
Lets run a quick bash command to create a new markdown file with a title.
echo "# github-ssh" >> README.md
It is a good idea to have a README.md file in all your projects. The purpose of the file is to help new and returning visitors to your repository get oriented. Your future self and collegues will thank you.
If you want a crash course on markdown syntax, check out the Official GitHub article titled Basic Writing and Formatting Syntax
Initial Commit
Congratulations! You have setup ssh authentication with GitHub AND created a git commit on a local repo.
Well, let’s trust but verify.
First, lets initialize the local repo.
git init -q
Now lets see the status of the repo.
git status
You can see we have one file that is untracked. Lets stage all new files and changes
git add .
When we look at the status again we can see all the files ready for commit.
git status
Commit the changes.
git commit -m "initial commit"
Check the git logs to confirm changes are being tracked.
git log
exit the logs by pressing q
Create a repo on GitHub
If we drop our laptop in a puddle we don’t want to lose our code!
Let’s push our code up to GitHub. First things first we need to create a repository on GitHub.
- Select a repo name
- Type in an optional description
- Select
Public
orPrivate
- Optionally select other settings
- Click the
Create repository
button
For the purposes of this tutorial we will choose private and not make any optional selections
Hook up your project to the remote
Congratulations! You have created a new repository on GitHub.
After creating the repo you will be given instructions for HTTPS and SSH; ensure you select the SSH option.
You will also be preented with a few options regarding new
or existing
repository. We want to follow existing
repository.
Add origin
git remote add origin git@github.com:USERNAME/REPO_NAME.git
Remember the ssh config we setup earlier? The host name needs to be setup as github.com
and the user needs to be setup as git
in order for ssh commands to work.
cat ~/.ssh/config
Hopefully you see something like this.
Host github.com
HostName github.com
User git
IdentityFile /path/to/ssh-key
...additional config
On our local machine change the branch name the local repo initialized to with the name main
.
git branch -M main
Push the local code up to the remote repo with upstream tracking.
git push -u origin main
Verify the remote for the project.
git remote -v
Delete project from local machine
Delete all your hard work. Fear not! The next step is to clone the GitHub repo back to your local machine.
cd .. && rm -rf github-ssh
Clone the repo
git clone git@github.com:__USERNAME__/__PROJECT_NAME__.git
Final Remarks
There is a lot more to learn about git and GitHub.
From here I would start digging into git branching and merging. Also, you can start looking into git integrations with code editors such as vscode.
Bye for now!