STAT 545 - Fall 2026
From this week’s topic, students are anticipated to be able to:
create an R Project,
use git on their own computer (locally),
connect between a local git repository and that repository on GitHub, using RStudio,
make commits in git using RStudio,
make a branch in git using RStudio or GitHub, and
use collaborative GitHub features such as Issues and pull requests.
After this class, you should be able to start working on your Collaborative Project.
Important
🚨 I need your GitHub username to add you to our STAT545 GitHub organization! 🚨
PLEASE INPUT YOUR GITHUB USERNAME INTO THIS CANVAS ASSIGNMENT BOX NOW
Thanks 😊
“Git” is a version control system designed to track changes.
Git essentially creates “snapshots” of your work as you progress through a project.
This is what GitHub uses!
A repository stores files and the history of the files; the usual convention is to use a single repository to organize a single project.
GitHub is a place where repositories can live online. Being online provides us a way to share and collaborate on projects. It also serves as a backup for your project.
Example 1: Grace’s GitHub page
Example 2: The STAT 545 GitHub page
The first repository lives under Grace’s personal Github account. The second repository lives under the UBC-STAT organization. Organizations are useful if you have lots of different projects with a common theme which lots of people are collaborating on.
We can interact with and send data between GitHub and RStudio using Git.
To use Git, we should first understand the primary states that files can be in. These are:
After changes are committed locally (on your computer), we can push them to a GitHub repository.
This sends the changes and updates the most recent version of your project on GitHub.
We can also pull changes from GitHub to your local machine. This is useful when multiple people (or perhaps one person on multiple devices) are working on a project.
Other useful Git features include:
We will demonstrate how Git and GitHub can be used within an R Project.
R Projects are useful for organizing files and resources specific to a project in a self-contained directory.
R Projects allow for more reproducible workflows, and allow users to resume exactly where they left off within a project by keeping variables in the environment even after the session has closed.
There are really two (okay, more than that but we’ll focus on two here) ways that you can connect RStudio and Github. You can:
Create a GitHub repository, and then sync that to an R Project in RStudio, OR
Create an R Project in RStudio, and then sync it to GitHub.
I will show you both ways.
Side note here: the newer version of RStudio (called Positron) does not use R Projects. Instead, you can create a “folder”. All other steps will be pretty much the same!
1.1: Go to your GitHub profile (mine is at https://github.com/grcetmpk)
1.2: Create a new GitHub repository by clicking the + symbol in the top bar and using the dropdown menu to create a new repository.
1.3: Name your repository “demo-github-first-[initials]”. In the description write “STAT 545 Demo”. Check the box for adding a README file.
You are now redirected to the repository main page. The repository page tells you you have 1 commit.
Click on it to get to the history page. This tells you all the changes that have been tracked for the files in the repository so far.
1.4: Go back to your repository main page. Click on README.md, then click the pencil icon (“Edit this file”). Add the following information into the README.md file:
Your Name
Your Program
1.5: Commit your changes by clicking the commit changes button, and briefly summarize the changes you’ve made in the Commit message.
Now, look at the GitHub commit history again. Has anything changed?
Now it’s time to turn the repository into an RProject. We do this by cloning.
2.1: In RStudio, go to “File > New Project”
2.2: Click on “Version Control: Checkout a project from a version control repository”
2.3: Click on “Git: Clone a project from a repository”
Now you have a local version of this project! And can make changes on both RStudio or GitHub, which is described in the “Syncing Your Changes” section below.
Side note:
1.1: Open RStudio.
1.2: Go to “File” > “New R Project…”.
1.3: Select “New Directory”, “New Project”, and name it “demo-rstudio-first-[initials]”.
RStudio will refresh. You will now see at the top of RStudio that the name of your project is displayed.
RStudio doesn’t automatically create a README, but we can add one quickly.
2.1: In the R Console, load in the usethis package by calling library(usethis)
install.packages("usethis") first)2.2: In the R Console, run use_git().
2.3: If you restarted RStudio, call library(usethis) again. Then, in the Console call use_github(). A new repository on your GitHub account should be created! You should see your file README.md as one of the items in your repository.
If you have issues, see the last section of Lecture 1A: Installation to set up your GitHub credentials on RStudio.
At this point, regardless of whether you started with RStudio or GitHub, the steps are the same.
One way or another, you have a GitHub repository connected to RStudio.
Now…. what happens when we edit our project on our computer? Or on GitHub? Well, we need to sync those changes, and spoiler alert: it’s not automatic.
If we made a change on RStudio, we need to PUSH it to GitHub.
If we made a change on GitHub, we need to PULL it to RStudio.
Let’s make some changes to one of our example projects! Let’s add a new R Script to your R Project
go to “File” > “New File” > “R Script”.
Write some code:
# view the first few rows of mtcars dataset
head(mtcars)
# calculate the average mpg among all cars, save it as a variable
mean_mpg <- mean(mtcars$mpg)
# print the mean mpg
mean_mpganalysis.R, and make usre it’s saved in your project folder!Note: After running this code, you’ll notice mean_mpg is in your environment. The cool thing about R Projects is that the next time you open your project, you’ll continue exactly where you’re left off! All variables you created will have been saved, as if you never closed R Studio.
You can add multiple files to projects, and save things like images/pdfs of plots, multiple R Scripts (code), and data sets. Really, anything that you can save in the R Project folder will be contained in the R Project.
Now we need to push this to GitHub.
There are two ways that I’ll show you how to interact with GitHub through RStudio.
Using RStudio’s “Git” Tab
Using the Terminal
(Secret third option: using GitHub Desktop. I won’t show this in detail but it is an option!)
When you navigate to the “Git” tab in RStudio, you will see that README.Rmd and analysis.R are listed. This is because these files have changes! README.Rmd was added, and analysis.R was modified.
We need to stage the files, commit the changes, and then push the changes to the remote GitHub version.
Push: You now have nothing to commit, as all changes have been tracked. You will see an information message saying “Your branch is ahead of”origin/main” by 1 commit. Time to Push! Click the Push button. If successful, the following message will appear:
>>> /usr/bin/git push origin HEAD:refs/heads/main
To https://github.com/<yourGitHubusername/STAT545_Example_Project.git
37b4ec8..7b32df4 HEAD -> mainClose this pop-up, and the “GitHub interface.”Review Changes” pop-up.
Now, when you go to your GitHub account, you should see some changes to your STAT545_Example_Project repository! You can see when the changes were pushed, and now a README appears below the files!
Instead of using the Git pane, we can write out commands for Git in the Terminal.
Stage Changes: git add . stages all of your modified files (can also just name the files you want to commit instead of using .)
Commit Changes: git commit -m "some message" commits your changes, along with a message summarizing the changes
Push Changes: git push origin main pushes your changes to the GitHub repo (main branch, which is typically the default)
Sometimes, files get edited directly on GitHub (or perhaps someone else changed some files, sent them to GitHub, and you don’t have those important changes on your computer!)
Let’s edit the README.md file on GitHub. Click the pencil icon at the bottom of the repository where the README appears, change or add some text, and click the green “Commit Changes” button.
The changes only exist on GitHub! If you look at your README.md file on your computer or in RStudio, it is not updated.
To get changes from GitHub to RStudio, we PULL. We do this by either:
git pull in the terminalAfter doing either of these, your changes should appear in your project!
Branches allow for parallel development within a repository. Branches are also a safe way to test out new features, fix bugs, or experiment with new ideas.
When creating a repository, a default branch is created. This is typically named “main” (or in older repositories, “master”).
The default branch is the branch that GitHub displays when anyone visits your repository.
The default branch is also the initial branch that Git checks out locally when someone clones the repository.
Unless you specify a different branch, the default branch in a repository is the base branch for new pull requests and code commits.


You can make changes on your new branch, and push them to that branch on GitHub.
git checkout -b <new-branch-name> and switch branches using git checkout <new-branch-name>. You can push changes to the new branch using git push origin <new-branch-name>, where <new-branch-name> is the name of your branchAfter pushing changes to a branch, you will see a message on GitHub.
If this message does not appear, click “main” and switch the branch to the new one you just made. You will see a message that says “this branch is one commit ahead of main”. Click that message.
Click “Compare and pull request”. Here, you can see the changes between the main and new branch.
If you’re satisfied with the changes, you can click “Create pull request” to begin the merge the changes from your new branch to main.
If no conflicts exist, click “Merge pull request”. Now, the changes will be on the main (default) branch!
Click “Confirm merge”.
Merge conflicts happen when we’ve created multiple versions of files that can’t be obviously combined into one definitive version.
Here is an example of something that would not cause a merge conflict:
At 9am, my TA pulls from the STAT 545 repository, makes a local change to the course dashboard, and commits and pushes her changes.
At 10am, I forget to pull from the STAT 545 repository, and start working on the Day 1 notes locally.
When I commit and push, Git is a bit confused, because I wasn’t working off of the “freshest” version of the STAT 545 repository - but since my TA and I were working on different lines of code, it will fairly seamlessly figure out that the right thing to do is to add my changes to the Day 1 notes to the current version of the STAT 545 repository online.
Here is an example of something that WOULD cause a merge conflict:
At 9am, my TA and I both pull from the STAT 545 repository.
At 10am, my TA changes the front page to say “STAT 555 @ UBC”, and commits/pushes those changes.
At 11am, I change the front page (without pulling!!!) to say “STAT 777 @ UBC”.
When I commit and push, Git doesn’t know what to do. Should it make the version that says “STAT 555 @ UBC” or “STAT 777 @ UBC” the definitive version? The push will fail, and Git will tell us we need to fix the conflict and then commit the result.
How do we fix this?
Pull.
Open the file that caused the merge conflict. You should see something like this:
<<<<<<< HEAD
STAT 555 @ UBC
=======
STAT 777 @ UBC
>>>>>>> 526363991d21ed20e7e0c57b5e99d944ac5ce5aa
The stuff below the <<<<<<< and above the ======= is what was in your local version; the stuff above the >>>>>>> and below the ======= is what was in the remote conflicting version. Decide what you want to have on the offending line (e.g. “STAT 555 @ UBC”), and replace the whole block of text above with that.
Save and commit the file. message here might be “Fixing a merge conflict.” You should now be able to successfully push!
Exercise
Find a partner. In this exercise, we will learn how to have different team members save their work separately on branches, and how to merge those changes to the main project branch.
Teammate A adds Teammate B to their “stat545-demo” repository as a collaborator. (Go to Settings from the main repo page, then Access > Collaborators). This should send Teammate B an email with a collaboration invitation; accept that invitation.
Teammate B clones Teammate A’s demo repository they made in class today/last class.
On their own computer, Teammate B will make a new branch in Teammate A’s demo repository. You can do this either on Github by clicking on “1 branch” on the repo homepage then the green “New branch” button, or on your own computer in R Studio with the “New Branch” button.
On their own computer, Teammate B will create a new file in the newly created branch, then commit and push it.
Teammate B will start a pull request (basically a request to merge content onto the main branch) on GitHub, by going to “Pull Requests” -> “New Pull Request”, and selecting the branch you intend to merge to the main branch. In this pull request, include a comment and title indicating (at a high level) what the change made is about.
Teammate A will follow the instructions here to merge the pull request.
Commit changes OFTEN! Commit them as often as you would save your work!
Be careful when working with data containing personal identifying information (PII). This should NOT be stored on GitHub.
When you watch a Github repo, you get notifications when things happen in them. So if you “Watch” the STAT 545 webpage repo, then you will get email notifications when I update the site!
Today’s class is a digest of the following resources:
Online tutorials:
The basic version control workflow (without branching): Happy git w R: Chapter 20.
Starting with GitHub: Chapter 15: New project, GitHub first
Starting with files on your computer, and didn’t set up git: Chapter 16: Existing project, GitHub first
Starting with files on your computer, and did set up git: Chapter 17: Existing project, GitHub Last
Some additional resources that you might find useful: