Using RStudio with version control will make collaboration smoother by keeping track of changes in Git. Furthermore, linking your local Git to your Github account’s repository will allow you to access your R code and files from anywhere.. Not only will your code be accessible on Github, but every change will also be logged and various versions of the same file can be created so that you can easily view and incorporate changes made by collaborators or go back to a previous version if a mistake is made.
Read more about version control here.
*This guide assumes that user has basic knowledge of RStudio and Github.
*You should already have a Github account. If you do not, create one at github.com.
*Your computer should have Git already installed.
To check in Windows, go to your start menu and search for 'Git.' If an item called 'Git Bash' shows up, you are good to go.
To check in Mac, go to Applications > Utilities > Terminal. Open Terminal and type 'git --version' and hit enter. If prompted to install command line developer tools, install as prompted. Then type in 'git --version' again and hit enter. If the version description of git shows up in Terminal, you have Git installed on your computer already.
If your computer does not have Git, go to https://git-scm.com/downloads to download the latest version.
1) RStudio Setup
A. Set RStudio to use Git Bash as shell - In RStudio, go to Tools > Global Options > Git/SVN > make sure boxes are checked for both “Enable version control interface for RStudio projects” and “Use git Bash as shell for Git projects”
2) Create New Repository & Project
1. Create a new repository in Github
a) Log into Github and create a new repository.
b) Copy the URL of your repository from your browser’s address bar
2. Create a new project from your Github repository in RStudio
a) In RStudio, go to File > New Project… > Version Control > Git
b) Paste your Github repo link into the Repository URL box
c) Optional: edit your Project directory name and select desired subdirectory for project
d) Click ‘Create Project’
3) Git Setup
1. Open Git Bash shell from RStudio - Tools > Shell...
2. Confirm that RStudio is using Git - The shell window name at the top should say something like 'MINGW64' or 'MYSYS.' To double check, you can type the following:
Hit enter. Your window and output should look something like this:
However, if your shell looks like the following, something is wrong.
If you have the wrong shell, check on the top right corner of your RStudio window to make sure that you are in the correct RStudio project. The RStudio project should be the one you created from a Github repository.
If you followed the previous instructions correctly, your shell should be Git Bash. Check all of the assumptions at the top of the guide. If all else fails, try going through the instructions of this guide again from the very beginning, making sure you do not skip any steps.
3. Configure Git
In your Git Bash shell:
a) Type the following, replacing Your Name with your actual name, and hit Enter:
git config user.name 'Your Name'
Git will use your name to keep track of the identity of the person who is making the changes to the files.
b) Type the following, replacing email@example.com with the email associated with your Github account, and hit Enter:
git config user.email 'firstname.lastname@example.org'
c) To check that Git has been properly configured, type the following:
git config --list
Hit enter and scroll to the bottom to make sure user.name and user.email are correct. If you would like to make any changes, repeat steps a) to c).
4) Committing and Pushing Changes
1. Commit changes to Git. To commit basically means to save your changes.
a) Check all of your windows and make sure your Git Bash shell window is not open. If it is, close the window by clicking the red ‘x’ at the top right corner.
b) Go to the Git tab on the top right panel of RStudio. Any changes you make to the project (e.g. edits of existing files, creation of new files, etc.) that have not been committed should show up in the panel.
If you made changes to files but you do not see those files appear in the panel, make sure 1) you’ve saved the files and 2) you have refreshed the Git panel by clicking the circular arrow at the top right corner.
c) Select the boxes under ‘Staged’ of the files you want to commit, then click ‘Commit’. An ‘RStudio: Review Changes’ window will pop up. Here, you can see the changes you’ve made since the last commit. Type a brief description of the changes in the ‘Commit message’ text box and click ‘Commit.’
2. Push changes to Github. The changes you made are now saved and available in the local Git on your computer, but it has not been made available online on Github. Pushing the changes will update Github so that all the changes committed in Git will be reflected in the files on Github.
a) To push changes from Git to Github, go to the Git panel of RStudio and click 'Push'
Alternatively, you can find this 'Push' button from the 'RStudio: Review Changes' from the previous step when you commit a change. Or, you can access this button from the Git drop-down menu on the top toolbar of RStudio.
b) Enter your Github login credentials as prompted. Your changes should be successfully pushed to Github if you see the following message:
5) Optional: SSH Key Setup
Setting up an SSH key that links Git from RStudio to Github allows you to avoid having to type in username and password every time you want to push an edit to Github.
1. Create key in RStudio
a) In RStudio, go to Tools > Global Options... > Git/SVN. Click ‘Create RSA Key…’. Another window will pop up; in that window, click ‘Create’.
An additional window will pop up after you click create; you can just click ‘Close’
b) Back in your Global Options window, you should now have a SSH RSA Key with the path directory shown. (It should look something like C:/Users/YourUsername/ .ssh/id_rsa). Click ‘View public key’ and copy the key.
2. Add key to Github
a) Go to Github and click go to your profile by clicking on your avatar, which produces a drop down menu. Go to Your Profile > Edit profile > SSH and GPG keys, and click ‘New SSH key.’
b) Give your key a title and paste the key into the Key textbox. Then click ‘Add SSH key’
3. Add Github repository as remote
a) Go back to RStudio and open up the Git shell (Tools > Shell…).
b) In the shell, type the following all in one line, replacing YourUsername with your Github username and YourRepo with the name of your Github repository:
git config remote.origin.url email@example.com: YourUsername/YourRepo.git
Note: The first time you push a change after setting up your SSH key, RStudio will prompt you with a warning as follows:
Type ‘yes’ in the textbox. After this, You will now no longer be prompted to type in your username and password every time you want to push a change to Github
If you’d like to read more about version control, go to: RStudio Support - Version Control with Git and SVN
This tutorial drew heavily from http://happygitwithr.com/, an excellent and comprehensive resource of RStudio and Git.