5  GithHub + RStudio

Happy Git and GitHub for the useR is a great resource for connecting Git/GitHub and RStudio.

Markdown Cheatsheet

5.1 Lab Specific Git + RStudio Communication

  1. Generate token
  • Log into https://github.com/
  • Go to https://github.com/settings/tokens
  • Press Generate new token
  • Select Generate New Token (Fine-grained, repo-scoped) from the dropdown menu
  • Create token name
  • No description
  • Select expiration
  • Keep public repositories
  • Don’t add any permissions
  • Click generate token
  1. Copy token to computer (notes, word, password organizer, etc…)

  2. Enter GitHub Token into R

  • Check if the “gitcreds” package is installed
    • at the prompt in the Console type ‘gitcreds()’
      • if an Error is received, please install package
install.packages("gitcreds")
  • Add your token to R
    • in the console at the prompt > run the command below
gitcreds::gitcreds_set()
  • Paste token when prompted

  • To check if this worked run “gitcreds::gitcreds_get()”

  • also make sure you have packages rmarkdown and knitr by searching in packages

Create a New Project in RStudio from an existing project on GitHub

Clone Repository

Create a branch in GitHub

For each GitHub repository you will be working on in the lab that is shared among others, you will need to create your own Branch.

Please create your own branch directly in the repository on the GitHub site

Create Pull Request, for review, on GitHub

6 Old Instructions

6.0.1 How to Connect Github and RStudio using a GitHub Access Token

6.0.1.1 Enter your email (used to log into R Studio) and username into RStudio

  • Navigate to the Terminal Tab in R Studio
  • Type the following in to the Command line

$ git config --global user.email "user@psu.edu"
$ git config --global user.name "First Last"

6.0.1.2 Each person needs to have their own or project branch

  • First request from the Lab Manager to create your own branch and provide the Name of the Branch (e.g. firstname_lastname).

  • The lab manager will create this branch on the Github repository site.

6.0.1.3 Enter your branch into the repository in Rstudio

  • Navigate to the Terminal Tab in R Studio
  • Type the following in to the Command line:

$ git branch -m <branchName>

6.0.1.4 Create an Access Token in Git

6.0.1.5 Clone a Git Repository to RSTudio on TLT Server

  • Navigate to the GitHub project page (e.g. https://github.com/gilmore-lab/student-testbed)
  • Press the button Code
  • Copy the HTTPS link of the project by clicking the little icon to the right of the URL. The link will be something like https://github.com/gilmore-lab/student-testbed.git

Clone with HTTPS
- Copy this to another blank page. This will need to be altered to the following: https://oath2:<PersonalAccessToken>@github.com/<repository>/<project>

  • The Personal Access Token created in Git will be used in the above location. https://oath2:<PersonalAccessToken>@github.com/gilmore-lab/student

6.0.1.6 Make changes to branch on repository

  • Ensure the branch name is listed in the Git window in R

Git Window in R
  • Always run ‘Git Pull’ before changes are made

  • Make Changes to File

  • Commit changes

  • Run ‘Git Push’ to Your Branch on Github

  • Go to github.com/gilmorelab/<repository name> and create a Pull request

  • Notify the lab manager that a pull request is available on GitHub for <repository name>

6.0.2 How to Access the Shared Space on R Studio

  • Go to the File menu and click on Open Project

    Open Project
  • Then you will see

    My Projects
  • Click on Shared with Me

    Shared Projects
  • Highlight the shared project you want to open and Click on Open Project

6.0.3 Configure RStudio for git integration

  1. From within RStudio, open the Tools menu and select the Global Options... item.
  2. Select the Git/SVN panel in the left column. It is near the bottom of the list.
  3. Check the Enable version control interface for RStudio projects checkbox at the top of the window.
  4. Click on the View public key link to see if you have a valid SSH RSA Key already.
  5. If the window opens and contains a long text string that begins with “ssh-rsa” and ends with “(lxclusterapps?)”, where <your_PSU_id> is your PSU ID (e.g., rog1), you are half-way home.
  6. If the window is opens and is empty, you must generate a new key:
    • Close the window and click on the Create RSA Key... button.
    • From RStudio, select the Terminal pane. This will bring up a command line with a prompt that looks something like this: bash-4.2$.
    • Type ssh-keygen.py and hit return. This will generate an SSH key.
    • Type ls ~/.ssh and hit return. You should see something that looks like this:
    bash-4.2$ ls ~/.ssh
    id_rsa  id_rsa.pub  known_hosts
    • The id_rsa.pub is a file containing a ‘public’ key that you can save on GitHub so that GitHub knows who you are when you connect to it from the PSU TLT RStudio server.
    • (Optional) type cat ~/.ssh/id_rsa.pub to see the contents of this key file.
    • Select Global Options... from the RStudio Tools menu again.
    • Select Git/SVN from the left panel.
    • Select the View public key link to view the SSH key you just generated.
    • Copy the key to the clipboard (command+C on Mac)
  7. Visit your account on GitHub. Rick’s is at https://github.com/rogilmore.
  8. Click on your avatar or photo in the upper right hand corner and select the Settings command.
  9. From the list at left, select the SSH and GPG keys panel.
  10. Look at the list of SSH keys.
  11. If there are no SSH keys…
    • Press the New SSH key button.
    • Paste the key you generated in RStudio into the space provided.
    • Add a title like PSU TLT or lxcluster
    • Press the Add SSH key button to save the key.
  12. If you see an existing key with PSU TLT or lxcluster you should already be ready to go.
  13. Return to RStudio, hit Apply to save your changes, and Ok to exit the Settings panel.
  14. Test the connection between RStudio and GitHub by cloning a repository.
    • Visit the lab protocols repository at https://github.com/gilmore-lab/protocols.
    • Click on the Clone or download button.
    • Confirm that the window title says Clone with SSH. If it does not, click on the Use SSH link on the upper right side.
    • Click on the clipboard icon to copy the git@github.com... link to the repository to your clipboard.
    • Switch back to RStudio.
    • Under the File menu, select the New Project option.
    • Select Version Control.
    • Select Git in the next window.
    • Paste the repository link you copies from GitHub into the Repository URL field.
    • Select a name for the local repository. I usually keep the name from GitHub, so in this case, I would use protocols.
    • Select a directory/folder where the repository will be copied to. I have an rstudio/ folder I use for this. If you need to create a folder, you may do so.
    • Click on the Create Project button. This will copy the repository from GitHub to your personal (PASS) file space on PSU’s servers.