Connecting RStudio to GitHub

Git GitHub Reproducibility

Gitting serious about version control

Erika Duan
05-30-2021

Introduction

This is a post about a housekeeping activity that keeps your coding projects reproducible and collaboration friendly. Automating version control may be an unglamourous activity, but it will prevent your team from sinking into a swamp of messy code.

Let’s talk about SSH

The story of connecting to a remote GitHub repository begins with a segue into the Secure Shell (SSH) protocol. SSH is a cryptographic network protocol that enables secure communication, such as file transfers and remote device management, between a local machine and a remote host over an open network.

SSH uses public-private key pairs to authenticate hosts to server communication. This involves:

Note: Using SSH keys allows you to connect to GitHub without having to supply your username and password each time you push to GitHub.

Create a new SSH key from RStudio

  1. Navigate to Tools > Global Options > Git/SVN and check whether you have an existing SSH RSA key.
  2. Click Create RSA key. RStudio will create an SSH key pair and store your identification and public key in the files ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub respectively.
  3. Click Apply and OK to close the Global Options window.

Note: You should always protect your private key with a passphrase as an additional layer of security. RStudio will also automatically add your key to the SSH agent, which saves you from entering your passphrase every time you use your SSH key.

  1. Navigate back to Tools > Global Options > Git/SVN and click View public key to copy your public key to clipboard (or an open text file). You may need to restart RStudio for your SSH key pair to be detected.

  1. Navigate to https://GitHub.com/ and log into your GitHub account. Click on your profile icon on the top right corner of your browser and navigate to Settings > SSH and GPG keys. Click New SSH key and paste your previously copied public key into the box.

  1. Add an informative title like the current year and device that you are using i.e. 2021-lenovo-lt and create your new SSH key. Congratulations! You have now connected to your remote GitHub repositories via RStudio.

Update GitHub repository from RStudio

  1. To check your remote connection, open a local Git repository and make some file changes. Navigate to the Git tab in RStudio, which is localised next to the Environment and History tabs. New file modifications will be marked by M or ? icons to denote modified file changes or newly added i.e. untracked files respectively.

Note: New file modifications will only be marked after your file changes are saved.

  1. Select all changes and press Commit. This will take you to a new window where you can review all file changes. Code addition is marked in green and code removal is marked in red. Track your new commit with a message describing your latest changes and press Commit.

  1. New commits are now reflected under the Git tab. To update your remote GitHub repository, press the green Push arrow to push your new commits into GitHub. Congratulations, you have successfully synced to your remote GitHub repository and made a new update!

Sync to a new GitHub repository From RStudio

According to the Happy Git and GitHub guide, the easiest way to sync to a new GitHub repository is to manually create and then clone your new repository from https://GitHub.com/. Doing this also establishes your remote connection, which allows you to immediately push or pull from your GitHub repository.

  1. First, create a new repository from https://GitHub.com/.

  1. Copy the SSH URL from GitHub (use the SSH connection as you have already set up your SSH keys in RStudio).

  1. In RStudio, navigate to File > New Project > Version Control > Git and paste your SSH URL as the Respository URL. Doing so should automatically populate the name of your project directory (which should be the same name as your GitHub repository). Select a local location for your new project and create your project. I recommend creating a folder which houses all your local git repositories and always using this as your new project location.

Note: This step is equivalent to typing git clone <SSH URL> on the command line. The first time I tried to clone a new Github repository using RStudio, I received a fatal error message that RStudio could not read from the remote repository. I resolved this by cloning the remote repository from the command line, which allowed me to continue even though the authenticity of host ‘github.com’ could not be established. Doing so permanently added ‘github.com’ (RSA) to my list of known hosts and resolved the error.

  1. Congratulations, you can now commit changes and push them to your remote GitHub repository!

RStudio Git usage notes

Other resources