git config --global user.name "YOUR FULL NAME"
Setting up Git and GitHub
Learning Objectives
- Installing git
- Setting up GitHub
- Connecting to GitHub with PATs
- Connecting to GitHub with SSH
Installing Git
Install git:
- Windows: http://git-scm.com/download/win.
- OS X: http://git-scm.com/download/mac.
- Debian/Ubuntu:
sudo apt-get install git-core
. - Other Linux distros: http://git-scm.com/download/linux.
Create an account on GitHub: https://github.com/
- A free plan is fine. Use the same email address you used above.
- When you choose your username, remember that you might use this for professional purposes. So I would recommend something related to your name (e.g. my handle is “dcgerard”). I would recommend against something like “david_awesome_david_yolo_hahaha”.
Tell git your name and email address. Open up a terminal and type:
git config --global user.email "YOUR EMAIL ADDRESS"
If you are worried about email privacy, then use “<username>@users.noreply.github.com” as your email address (where you have replaced <username> with your GitHub username), then follow the instructions here to make your email address private on GitHub.
- Choose your favorite editor to be your default
Nano (a simple text editor):
git config --global core.editor "nano -w"
Emacs (for the brave)
- On Mac: https://emacs-modified.gitlab.io/macos/
- On Windows: https://emacs-modified.gitlab.io/windows/
git config --global core.editor "emacs"
HTML Authentication
To integrate GitHub (the website) with git (the command-line program), you have to set up the proper authorization.
If you can already integrate with GitHub through the command line then you can skip the rest of these notes. If you are not sure, then please continue.
There are two ways to authenticate. This section covers personal access tokens (PAT), the next covers SSH keys.
GitHub recommends access tokens, so please try that first. Only try SSH if the access tokens don’t work for you.
These notes follow the directions here: https://usethis.r-lib.org/articles/git-credentials.html
Create a personal access token by running this in R:
::create_github_token() usethis
This opens up a browser to GitHub.
Change the “Note” to describe the machine it is used on. E.g., “Personal Macbook”
You can change the expiration date.
- You have to generate a new token every time yours expires and go back through this setup.
- GitHub does not recommend using a non-expiring token. For this class, I would recommend choosing “custom” then a date after this class has ended
The default scopes should be fine for this course.
Click “Generate Token”
Do not close this window until we are done. It has your PAT which you won’t be able to see again.
- Do not share your PAT with anyone. Treat it like a password.
Copy the PAT
Run this in R:
::gitcreds_set() gitcreds
Paste your PAT in the console
Verify that your token is setup by running this in R:
::gh_token_help() usethis
- In the future, make sure all cloning URLs are like this:
https://github.com/<OWNER>/<REPO>.git
- Not like this:
git@github.com:<OWNER>/<REPO>.git
- Not like this:
SSH Authentication
Only continue on if the HTML authentication did not work for you.
An alternative way to authenticate is via SSH.
SSH uses what’s called a key pair where GitHub has a “public key” (a very long publicly known password) and you have on your computer a “private key” (a very long privately known password).
Think about the public key as a padlock, and the private key as the key. You have to use to private key to unlock the public key.
You need to generate a new private key, and go through the below steps, for each computer for which you intend to use git.
Generate Key Pair on R Studio
- R Studio makes it easy to generate a key pair.
Check to see if you already have an SSH key pair by running the following in R
file.exists("~/.ssh/id_rsa.pub")
It will return
FALSE
if you do not have a key pair.Open up Tools > Global Options… > Git/SVN.
If you do not have an SSH key pair, then click on “Create SSH Key…” and follow the prompts.
Click on “View public key”
Copy the entire text that shows up. This is your public key.
Go to Add Public Key to GitHub to continue authentication setup.
Generate Key Pair on the Terminal
- If the R Studio pipeline does not work, then try generating your key pair on the terminal with the following instructions.
Check for existing SSH Keys
In the terminal run
ls -al ~/.ssh
This will say something like “ls: cannot access ‘/c/Users/Vlad Dracula/.ssh’: No such file or directory” if you don’t have any public/private key pairs.
This will list out files names like “id_rsa.pub”, “id_ecdsa.pub”, or “id_ed25519.pub” if you do have a public/private key pair.
Generate a new SSH key
If you do have an SSH key, go to the next section (“Add an SSH key to the ssh-agent”)
If you don’t have an SSH key, follow the below steps.
In the terminal, run
ssh-keygen -t ed25519 -C "your_email@example.com"
Press enter if prompted where to save the key to accept the default location.
At the prompt, type a password that you can remember.
Add an SSH key to the ssh-agent
In the terminal, run the following to start the ssh-agent in the background.
eval "$(ssh-agent -s)"
In the terminal, run the following to add your SSH private key to the ssh-agent:
::: {.cell layout-align="center"}
```{.bash .cell-code}
ssh-add ~/.ssh/id_ed25519
```
:::
Copy Public Key
Run the following in the terminal to show the contents of “id_ed25519.pub”.
cat ~/.ssh/id_ed25519.pub
Highlight the output using your mouse and copy the contents.
Add Public Key to GitHub
On GitHub, in the upper right corner, click on your profile photo and click on “Settings”
On the left sidebar, click on “SSH and GPG keys”
Click on “New SSH key”
In the title field, choose a descriptive title, like “Personal Laptop”.
Paste your key into the “Key” field.
Click “Add SSH key” and confirm your GitHub password.
Test to see if it worked
Enter the following in the terminal:
ssh -T git@github.com
Type “yes” if prompted to continue connecting.
You are successful if you see something like
Hi dcgerard! You've successfully authenticated, but GitHub does not provide shell access.