How to use GitHub for collaborative RStudio projects
In this highly simplified 3-step tutorial, we will provide a minimal guide on how to use the online platform GitHub for collaborative RStudio projects, i.e., to share code, data, and replication material or to collectively work on a research paper project. No previous knowledge required. For further tutorials, hints, and tips, see for example here.
Keep in mind: Git is a very powerful version control tool and we only scratch on the surface here! Using GitHub is not always very intuitive - especially as beginner - but it is a great and highly efficient collaboration experience once you are all set!
1 Mastering the prerequisites on/with GitHub and Git
1.1. Sign up at GitHub, it is for free (choose your user name wisely, see why here)
1.2. Create a new repository (and invite potential collaborators)
1.3. Install git on your local machine
2 Set up an R project with version control
2.1. If not already done, install R and R Studio on your laptop
2.2. Set up your credentials for interacting with GitHub
Open RStudio and in your RStudio Console, execute:
usethis::create_github_token()
- Scroll down in the git browser window and click “Generate token”
- Copy the generated PAT to your clipboard
- to store the PAT, execute
gitcreds::gitcreds_set()
and paste your PAT to the console and clickEnter
- Now, you should be all set to pull and push from RStudio to GitHub
2.3. Create an R project for your collaborative project with version control:
In R Studio go to,
File > New Project >
Version Control
Choose
git
Give your project a directory name, indicate the
url
of your newly created GitHub repository (or, alternatively, theurl
of your collaborator’s repository)Click
Ok
toclone
the repository to your local RStudio project.
3 Commit and track changes in RStudio (Pull/Push to GitHub)
3.1. In the top right corner of your RStudio project, go to the git
panel
3.2. ALWAYS click Pull
first to clone and track changes previously pushed to your GitHub repository (e.g., from collaborators)
3.3. Once you are done with your most recent changes, click on Commit
, review your changes, Stage
them by clicking all boxes for accepted changes, and confirm with Commit
.
3.4. Once Commit
is done, you still have to push changes (click Push
). Only then, your changes are sent to your GitHub repository.
🎉 Well done, you are set to enter the next level of co-authoring workflows!