Using Git in RStudio

The hard part of using Git in RStudio is getting it set-up.

  1. Telling RStudio where git.exe resides.
  2. Telling RStudio how to communicate with GitHub.

Go to the set-up instructions and scroll down to the section on how to set-up RStudio with Git. Follow instructions carefully and slowly.

Creating a local repository and then getting it on GitHub (or GitLab)

Note. Creating the repo locally first often leads to misery in my experience. Often safer to start by making a blank repo on GitHub or GitLab.

Let’s create a local repository

  1. Create a RStudio project (upper right with the blue cube with R). New project… > New Directory > New Project

  2. Check the box that says Create a git repository. You have made a local repository.

  3. Add a file. File > New File > R Script

  4. Type something and then save the file.

  5. Click the Git tab in the upper right.

  6. Click the changes that you want to commit (do all). And click Commit. Add a comment: first line is subject, newline, description (options).

  7. Repeat 4-6 a few times.

  8. Now look at the history. The little clock-like icon (or History in the Git window).

    • Click on filter by file to see just the changes to one file.
    • Do a search in the upper right.
    • Click on an early commit and click View file @...

Now let’s get this on GitHub (similar strategy if you are working on GitLab)

  1. Go to GitHub and create a repo with the same name
  2. Don’t select add Readme or anything
  3. A page that tells you how to get started should show up. Look for this code.

…or push an existing repository from the command line

git remote add origin https://github.com/<youraccount>/<reponame>.git
git push -u origin master
  1. Copy the code and go back to RStudio.
  2. Click the cog on the Git tab and click Shell
  3. Paste the code into the command line.
  4. If you see something about Keychain, try Deny. It will hopefully ask you for your GitHub account info on the command line.
  5. If it all works, you should now be able to push.

Methods for downloading repositories

If you want to copy someone’s repository on GitHub or make a copy of one of your own, there are a few approaches that you can take. I am going to use https://github.com/RVerse-Tutorials/Test as the repository that you want to copy and we will put the repository at Documents/GitHub/Test.

Method #1 Fork it

  1. Navigate to https://github.com/RVerse-Tutorials/Test on GitHub and click the ‘fork’ button in the upper right. Follow the instructions.

Now you need to get it on your computer.

  1. From RStudio, go to the menu option ‘File->New Project…’. Then from the resulting dialog, choose “Version Control”. Then choose “Git”. Then it asks for a “repository URL”. Supply this: https://github.com/<your github account>/Test and leave the “Project Directory Name” empty. And then choose a directory in which to put the new repository and click OK.

If you are using GitHub Desktop, you need to add it to that too.

  1. From GitHub Desktop, go to the menu option ‘File->Add Local Repository’. Then navigate to ~/Documents/GitHub/Test and add.

The problem with forking is that you can’t fork your own repositories and if you fork someone else’s repository, your fork will always show theirs as a ‘parent’—which doesn’t really have a bad effect but I rarely want my copy of someone’s repo to be linked to theirs in any way. Normally forking is used when you are contributing to the parent repository and need a copy to work on.

Method #2 Copying my OWN repos (not someone else’s)

This method is pretty failsafe and I find easy to remember. This how I normally copy one of my own folders and make a new GitHub repo. I don’t use Git at the command line very often and rather than looking up how to do it, getting something wrong 3x and then researching how to debug the problem, and then finding I’ve spent the morning with a simple task…. I just do the following now.

  1. Make a new repo (say Test) on GitHub and click the button to add a Readme.md file.
  2. Download and link to the new repo using Step 2 above (in Method #1).
  3. Add the repo to GitHub Desktop using Step 3 above (in Method #1)
  4. Copy the files (on my computer) from the repo I want to copy into the new folder. Note this works because I already have the files I want on my computer.
  5. Push these changes to GitHub, though the Git tab in RStudio or GitHub Desktop (my preference).

Method #4

  1. From RStudio, go to the menu option ‘File->New Project…’. Then from the resulting dialog, choose “Version Control”. Then choose “Git”. Then it asks for a “repository URL”. Supply this: https://github.com/RVerse-Tutorials/Test and leave the “Project Directory Name” empty. And then choose a directory in which to put it and click OK.

This method will have your new repository associated with the repository from where you downloaded. If it is not your own GitHub repository, you don’t want this because you will not be able to push changes to GitHub.

  1. Click on the ‘More’ link in the Git Window of RStudio, and click ‘Shell’. Then issue this command.
git remote rm origin

This detaches the cloned repository from the remote repository on GitHub from where you cloned it.

  1. Add the new repository to GitHub Desktop and then publish to GitHub. From GitHub Desktop, go to File>Add Local Repository and then navigate to ~/Documents/GitHub/Test and add.

  2. Publish to GitHub. Once you do step 3, a ‘Publish to GitHub’ button will appear. Click that to publish to GitHub.

Note, there are Git commands you could use to do steps 3 and 4, but in this workshop we are using GitHub Desktop.

Alternate Method #5

Another way is to download the repository as a zip file and unzip. Watch a video that shows you how to do this

Go to https://github.com/RVerse-Tutorials/Test and click ‘Clone or download’ and chose ‘Download Zip’. Unzip and you’ll probably want to remove ‘master’ added to the end of the repository name.

If you chose, ‘Clone in Desktop’, you’ll need to open a terminal window, navigate to the new folder you just downloaded, and run the git code git remote rm origin from within the terminal to detach the repository from the RVerse-Tutorials GitHub account.

Packages with C++ code

We will not be doing this in this short course, but often you will want to install packages with C++ code. To do that, you will need Rtools (Windows) or Xcode (Mac)

  • Rtools Windows users Rtools also so you can build packages with C++ code if needed. See comments here about changing path. Rtools

  • Xcode Mac users Open terminal and type the following command xcode-select --install


NWFSC Math Bio Program