This lesson is still being designed and assembled (Pre-Alpha version)

Collaborating via Github

Overview

Time: 15 min
Objectives
  • Authenticate with Github.

  • Connecting your project to Github.

  • Make changes locally and push them to Github.

In episode 5 we learned about using version control as you write your publication. In this part of the workshop we’ll setup Rstudio to authenticate with Github which is necessary to push your changes to Github.

Terminology: Git Push and Pull

Definition: The process of syncronizing your local git repository with your git repository on Github (or other Git server).

Authenticating with GitHub

Github used to allow simple Username & Password authentication but now Github requires a more secure method of authentication. There are two options to authenticate: Personal Access Tokens (PAT) and SSH. This workshop outlines both methods for authentication.

PAT

If this is the first time you are using Git in RStudio, when you push to GitHub from the RStudio IDE, you’ll likely be met with a login request:

On a Windows device it will look like this:

sign in GitHub Windows

on a Mac it will look like this:

sign in GitHub Mac

Ok, we are going to use PAT

Go to GitHub page, click on your icon/picture in the top right-hand corner, Go to Settings > Developer Settings > Personal Access Tokens and Click on Generate new token . Add a note on what it’s for, choose expiration, and scope. Click generate token at bottom of page. Once done, copy the token and save it somewhere - it will not display again on GitHub after this point.

Ok, back to RStudio:

On Windows navigate to the Token tab of the login:

Windows PAT login

paste in your token

On Mac:

add your GitHub username when prompted:

Mac GitHub username prompt

And when asked for a password in the next prompt, paste in your PAT:

Mac GitHub password prompt

SSH

For this workshop we’ll be using the SSH Key method. If you’ve used SSH authentication before with some other program or service you can likely use your existing keys. If you do not yet have a pair of SSH keys you can create them using RStudio.

  1. Check for your local SSH keys. Tools –> Global Options –> Git/SVN.
  2. If your “SSH RSA key” field is blank then you do not yet have SSH Keys and you should generate keys by clicking the “create RSA key” button. Set your passphrase and don’t forget it. Store your passphrase in your password manager.

Click “View Public Key” in RStudio –> Tools –> Global Options –> Git/SVN and copy the public key cypher text.

Now that you have a public key you need to add it to your account on Github.

  1. Login to your Github account with your web browser. https://github.com
  2. If you have not yet added your Public SSH key to your Github account in “Setting –> SSH and GPG keys” do so.

Click the “New SSH Key” button in Github then paste it into the form.

Now you can authenticate with Github using your SSH keys. Test this in the terminal with:

ssh -T git@github.com

Getting your repository’s URL from Github

You can get the address of your repository from Github by navigating to your repository on Github.com and clicking the green “Code” button. Make sure to copy the SSH form of the URL.

With that address you can complete setting the origin URL in the next step.

Checking and Setting the “Origin” for the local copy of yoiur repository.

If you forked and cloned the demonstration publication for this workshop as covered in an earlier episode then your copy of the repository should already have the “origin” set. Once the “origin” is set properly you should be able to push and pull your changes to and from Github. When you clone a repository from Github your local copy of the repository should have Github set as the “origin”. When you’re using SSH to authenticate with Github you’ll need to use the SSH form of your repository’s URL as your “Origin”

You can check this in Rstudio –> Tools –> Project Options –> Git/SVN

If the “Origin” field has the HTTPS form of your repository’s URL then you’ll need to update it from the terminal with command like:

git remote set-url origin <paste your repository address here>

Be sure to put your Github username in the URL.

After you’ve updated the Origin URL from the command line go back to R Studio –> Tools –> Project Options –> Git/SVN to verify you have the SSH form of the URL in your “Origin” field. It should look something like this.


If the “Origin” field is blank then you’ll need to add it from the terminal with a couple of terminal commands like this:

git remote add origin <paste your repository address here>
git fetch --set-upstream origin main

Push your local changes up to your repository Github.

With authentication set up and your local copy of your repository pointing to Github as the “Origin” you should be able to make changes and push them up to Github. Let’s try it and see if it works.

Challenge: Push to Github

  1. Make a change to one of the files in your project or add a new file.
  2. In R Studio’s Git panel check the box to Stage the changed file.
  3. Commit the change to your Git repository.
  4. Click the green up arrow to Push you repository changes up to Github.
  5. Look on Github.com to verify your changes are there.

With the ability to synchronize your changes between Github and your local the next step is explore options for publishing your research paper.

Key Points

  • Setting up R Studio to authenticate with Github using SSH Keys.

  • Setting the Git repository Origin in your R Studio project enables pushing and pulling from your local copy of the repository to the repository on Github.