How to Use Git/GitHub with R (2024)

This blog post has gotten so much interest that I’ve turned it into a full-length course. Check it Out

Using Git and GitHub alongside RStudio has the power to revolutionize how you work in R. But getting everything set up can be a challenge. Join me as I walk through everything you need to do in order to use Git and GitHub alongside RStudio.

But first, some background ...

What is Git? What is GitHub?

I remember when I was starting out learning R, Git and GitHub were things I had heard about, but only vaguely understood. I had a sense that they were about collaboration and sharing code, but beyond that … 🤷

So … what are Git and GitHub? First of all, they are two separate things:

  1. Git is open source software for version control. Using Git, you can do things like see all previous versions of code you’ve ever created in a project.

  2. GitHub is the most popular service (others include GitLab and BitBucket) for collaborating on code using Git.

It’s possible to use Git without using GitHub, though most people combine the two. Being able to have a record of all of the changes you’ve ever made to your code both locally and on a remote website is powerful.

Why Should I Use Git and GitHub?

I have seen three major motivations for people to adopt a Git/GitHub workflow:

  1. Using Git and GitHub serves as a backup. Because GitHub has a copy of all of the code you have locally, if anything were to happen to your computer, you’d still have access to your code.

  2. Using Git and GitHub allows you to use version control. Ever had documents called report-final.pdf, report-final-v2.pdf, and report-final-v3.pdf? Yes, yes, you have. Instead of making copies of files over fear of losing work, version control allows you to see what you did in the past, all while keeping single versions of documents.

  3. Using Git and GitHub makes it possible to work on the same project at the same time as collaborators. Many of the teams I train that are learning R decide to switch to Git/GitHub after collaborating using Dropbox, Google Drive, OneDrive, or the like. The problem they run into is that only one person can work on an RStudio project shared in this way. Git and GitHub have built-in tools that enable simultaneous asynchronous work, a major benefit for those working in teams.

The best resource I’ve found for understanding Git and GitHub comes from this 2016 talk (slides here) by Alice Bartlett of the Financial Times (hat tip to Garrick Aden-Buie of RStudio for telling me about it).

How to Set up Git

Now that you have a bit more of an understand of what Git and GitHub are, let's talk about how to set everything up. Much of what I'll share comes from the excellent book Happy Git with R by Jenny Bryan and Jim Hester. However, at the time of writing (February 2021), some things have changed with regard to credentials. I lay out what I believe is the most up-to-date advice for getting everything set up.

Install Git

The first step is to install Git. Chapter 6 of Happy Git with R lays out the process for Mac, Windows, and Linux users. I'm on a Mac so Git came pre-installed on my computer. I was able to verify that I had Git installed using the terminal in RStudio.

Configure Git

The next step is to configure Git. This is covered in Chapter 7 of Happy Git with R, though I show what I believe to be a slightly easier process. Specifically, I suggest using the edit_git_config() function from the usethis package, which will open your gitconfig file. Add your name and email and close this.

Initialize a Git Repository

Now that you’ve installed and configured Git, you can use it locally. The use_git() function will add a Git repository (often referred to as a “repo”) to an existing RStudio project. Here I’ll create a new project and then initialize a Git repo.

View Commit History

Now that my RStudio project has an associated Git repository, I'll see an extra tab on the top right: the Git tab. From here, I can see the entire history of changes to my code over time (not many yet!).

Make a Commit and View More History

Git doesn't automatically track changes the way a tool like Google Docs does. Instead, you have to tell Git: I made changes and I want you to keep a record of them. Telling Git this is called making a commit and you can do it from within RStudio.

Each commit has a commit message, which is helpful because, when you look at your code history, you see what you did at each point in time (i.e. at each commit). RStudio has a built-in tool to view your code history. You can click on any commit to see what changed, relative to the previous commit. Lines in green were added; lines in red were deleted.

Connect RStudio and GitHub

The process so far has enabled us to use Git locally. But what if we want to connect to GitHub? How do we do that?

Sign up for GitHub

The first step is to sign up for a (free) GitHub account.

Create a Personal Access Token (PAT) on GitHub

Once you’ve signed up, you’ll need to enable RStudio to talk to GitHub. The process for doing so has recently changed (this is where I see the largest major difference from Happy Git with R). The best way to connect RStudio and GitHub is using your username and a Personal Access Token (PAT). To generate a personal access token, use the create_github_token() function from usethis. This will take you to the appropriate page on the GitHub website, where you’ll give your token a name and copy it (don’t lose it because it will never appear again!).

Store Personal Access Token to Connect RStudio and GitHub

Now that you’ve created a Personal Access Token, we need to store it so that RStudio can access it and know to connect to your GitHub account. The gitcreds_set() function from the gitcreds package will help you here. You’ll enter your GitHub username and the Personal Access Token as your password (NOT your GitHub password, as I initially thought). Once you’ve done all of this, you have connected RStudio to GitHub!

How to Connect RStudio Projects with GitHub Repositories

Now that we've connected RStudio and GitHub, let's discuss how to make the two work together. The basic idea is that you'll set up projects you create in RStudio with associated GitHub repositories. Each RStudio project lives in a single GitHub repo.

How do we connect an RStudio project to a GitHub repo? Happy Git with R goes over three strategies. I'll demonstrate two of them.

RStudio First

Sometimes you already have a project locally and you want to get it on GitHub. To do this, you’ll need to first use the use_git() function from usethis, as we did above. Then, you can use the use_github() function, which will create a GitHub repo and connect it to your current RStudio project.

GitHub First

The most straightforward way to use RStudio and GitHub together is to create a repo on GitHub first. Create the repo, then when you start a new project in RStudio, use the version control option, enter your repo URL, and you're good to go.

General Workflow

Now that we've connected RStudio and GitHub, we can push and pull our work between the two.

Push

Pushing means sending any changes in your code from RStudio to GitHub. To do this, we first have to commit. After committing, we now have a push button (the up arrow) on RStudio that we can use to send our code to GitHub.

Pull

The opposite of pushing is pulling. Using the down arrow button, RStudio goes to the GitHub repo, grabs the most recent code and brings it into your local editor. (Pulling regularly is extremely important if you're collaborating, though if you're the only one working on an RStudio project and associated GitHub repo, you know your local code matches what's on GitHub so it's less important.)

You Did It!

You're now all set up to use Git and GitHub with RStudio!

https://giphy.com/gifs/filmeditor-high-five-sacha-baron-cohen-l0ErFafpUCQTQFMSk

Learn More

If you’re looking to learn more and I haven’t yet beat into you the idea that you should check out Happy Git with R, let me try it once more. It’s the best book to help guide you as you go deeper with using Git/GitHub in your R work.

Additionally, materials from a 2019 rstudio::conf workshop titled “R for Excel Users” cover “version control and practice a workflow with GitHub and RStudio that streamlines working with our most important collaborator: Future You.”

How to Use Git/GitHub with R (2024)

FAQs

How to use Git with r? ›

In RStudio, go to File > New Project, and choose “Version Control”, select “Git”, and type the repository URL found in your copy of the repository listed in the right column on the GitHub website.

How to connect to GitHub repository from r? ›

In RStudio, start a new Project: File > New Project > Version Control > Git. In “Repository URL”, paste the URL of your new GitHub repository. It will be something like this https://github.com/jennybc/myrepo.git .

How do I use R code from GitHub? ›

The most straightforward way to use RStudio and GitHub together is to create a repo on GitHub first. Create the repo, then when you start a new project in RStudio, use the version control option, enter your repo URL, and you're good to go.

How to use Git with GitHub? ›

How to Start Using Git and GitHub
  1. Step 1 – Install Git. ...
  2. Step 2 – Create a GitHub Account. ...
  3. Step 3 – Connect your GitHub account to your Git account. ...
  4. Step 4 – Create and edit your code files locally.
  5. Step 5 – Create a repository on GitHub. ...
  6. Step 6 – Push your local code to GitHub.
Sep 26, 2022

How do I push from R to GitHub? ›

Commit and push the changes to GitHub
  1. In RStudio click the Git tab in the upper right pane.
  2. Click Commit .
  3. In the Review changes view, check the staged box for all files.
  4. Add a commit message, for example Add initial speed and distance report .
  5. Click Commit .
  6. Click the Pull button to fetch any remote changes.
Feb 10, 2018

What is the correct way to use Git? ›

How Git works
  1. Create a "repository" (project) with a git hosting tool (like Bitbucket)
  2. Copy (or clone) the repository to your local machine.
  3. Add a file to your local repo and "commit" (save) the changes.
  4. "Push" your changes to your main branch.
  5. Make a change to your file with a git hosting tool and commit.

How do I load an R package from GitHub? ›

How to install R packages from GitHub?
  1. Step 1: Install the devtools package. To install a R package, start by installing the devtools package. ...
  2. Step 2: Install the package of interest from GitHub. ...
  3. Step 3: Load the package.

How do I connect to my GitHub repository? ›

In the top right corner of GitHub, click your profile photo, then click Your profile. On your profile page, in the header, click the Packages tab. Search for and then click the name of the package that you want to manage. Under your package versions, click Connect repository.

How to clone a Git repository in RStudio? ›

Clone your forked repository to obtain a local copy of the files .
  1. In RStudio , go to “File > New Project”
  2. Click on “Version Control: Checkout a project from a version control repository”
  3. Click on “Git: Clone a project from a repository”
  4. Fill in the info: URL: use HTTPS address.

How to check if RStudio is connected to GitHub? ›

To see if you have Git installed on your system, open RStudio and select Global Options from the Tools menu (Tools > Global Options…). In the dialog that opens, click the Git/SVN tab on the left-hand side of the pop-up window. Near the top of the pane, there is a field for the Git executable.

How do I publish an R project to GitHub? ›

Push an existing RStudio project to GitHub
  1. Open GitHub in your web browser.
  2. Create a new repository. ...
  3. Copy the two lines of code from the box labeled Push an existing repository from the command line.
  4. Open an existing project in RStudio. ...
  5. Open a shell by clicking Tools -> Shell.

How do I run code directly from GitHub? ›

Clone a GitHub repo and then open a project

On the start window, select Clone or check out code. Enter or type the repository location, and then select Clone. Visual Studio opens the project from the repo. If you have a solution file available, it appears in the "Solutions and Folders" fly-out menu.

Do I need both Git and GitHub? ›

In addition to its main website, GitHub features a desktop version that can be installed on local computers to help synchronize code. It should be noted that Git can be used without GitHub, but GitHub cannot be used without Git. (GitHub primarily was built to work correctly with Git.)

How to use GitHub as a beginner? ›

  1. Step 1: Create a repository. The first thing we'll do is create a repository. ...
  2. Step 2: Create a branch. Branching lets you have different versions of a repository at one time. ...
  3. Step 3: Make and commit changes. ...
  4. Step 4: Open a pull request. ...
  5. Step 5: Merge your pull request.

What is the difference between Git and GitHub? ›

The key difference between Git and GitHub is that Git is a free, open source version control tool that developers install locally on their personal computers, while GitHub is a pay-for-use online service built to run Git in the cloud. Git is a piece of software. GitHub is an online SaaS service.

How to create R package with Git? ›

Create a package with R Studio

Click “File” -> “New Project…” -> “New Directory” -> “R Package”. This brings up a menu where you give your package a name, and specify where to create it on your hard drive. To enable Git (Vuorre and Curley 2018), make sure that the “Create a git repository” box is checked (see below).

How to get Git tab in RStudio? ›

Starting from an Existing GitHub Repository
  1. Create a new RStudio Project: File > New Project –> Version Control –> Git. Copy the URL for the repository on GitHub into the dialog box. ...
  2. When the project opens, you should see the files in the Files tab and the Git tab should be visible in RStudio.
  3. You're all set up.
May 11, 2022

How to use Git rm command? ›

git rm
  1. <filename> The name of a file (or multiple files) you want to remove. ...
  2. --cached. Removes the file only from the Git repository, but not from the filesystem. ...
  3. -r. Recursively removes folders. ...
  4. --dry-run. No files are actually removed.

How to use GitHub token in R? ›

Here are the steps briefly via R:
  1. Create a GitHub PAT via R with usethis::create_github_token() (this will open a page in your browser pre-populated with the appropriate scopes. ...
  2. Copy and store the token in a password manager (Lastpass, 1Password)
  3. Back in R, use gitcreds::gitcreds_set() to register your token with git.
May 17, 2024

References

Top Articles
Pandesal
Knorr Sinigang sa Sampalok Mix 1kg
Canvas Rjuhsd
Bez.talanta Leaks
Aarf Anchorage Alaska
Ncqa Report Cards
Victoria Tortilla & Tamales Factory Menu
Use Caution: Herds of wild horses escaping Davis Fire spotted evacuating up Geiger Grade
Ups Open Today Near Me
Getwush Com
Farmers And Merchants Bank Broadway Va
Smart fan mode msi, what's it for and does it need to be activated?
National Weather Denver Co
1800Comcast
Pixel Speedrun Unblocked Games 76
Www.jetnet.aa.com
Dovob222
Food King El Paso Ads
Red Lobster cleared to exit bankruptcy under new owner Fortress
Kentucky Lottery Scratch Offs Remaining
Myzynrewards
Used Travel Trailers Under $5000 Craigslist
Guide:How to make WvW Legendary Armor
1970 Baltimore Orioles World Series Scroll Pennant
Fedex Express Ship Center
Lox Club Gift Code
Otter Bustr
Western Lake Erie - Lake Erie and Lake Ontario
Best Hair Salon Dublin | Hairdressers Dublin | Boombae
Philasd Zimbra
7066642123
Honeywell V8043E1012 Wiring Diagram
Metalico Sharon Pa
Rwby Crossover Fanfiction Archive
Plus Portal Ibn Seena Academy
Re/Max Houses For Sale
Body made of crushed little stars - Sp1cy_Rice_W1th_J4S - 僕のヒーローアカデミア | Boku no Hero Academia
Star Wars Galaxy Of Heroes Webstore
Fuzz Bugs Factory Number Bonds
Concord Mills Mall Store Directory
EnP. Karl Sam Maquiling on LinkedIn: #anniversary #localgovernment #urbanplanning #goodgovernance…
Alles, was ihr über Saison 03 von Call of Duty: Warzone 2.0 und Call of Duty: Modern Warfare II wissen müsst
Left Periprosthetic Femur Fracture Icd 10
Us 25 Yard Sale Map
Busted Newspaper Zapata Tx
Osrs Nex Mass
Tyler Sis Ferg
Lakeridge Funeral Home Lubbock Texas Obituaries
Santa Rosa Craigslist Free Stuff
Rs3 Spectral Spirit Shield
Birmingham National Weather Service
Backrooms Level 478
Latest Posts
Article information

Author: Cheryll Lueilwitz

Last Updated:

Views: 6189

Rating: 4.3 / 5 (54 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Cheryll Lueilwitz

Birthday: 1997-12-23

Address: 4653 O'Kon Hill, Lake Juanstad, AR 65469

Phone: +494124489301

Job: Marketing Representative

Hobby: Reading, Ice skating, Foraging, BASE jumping, Hiking, Skateboarding, Kayaking

Introduction: My name is Cheryll Lueilwitz, I am a sparkling, clean, super, lucky, joyous, outstanding, lucky person who loves writing and wants to share my knowledge and understanding with you.