The figure shows the states of a file: untracked or tracked. When it is tracked by the repository, the file can be staged, committed or modified. With the add action the file is “staged”, with the commit action the file is “saved” to the repository. This cycles repeats teach time the file is modified.
Our workflow
Concept model of a workflow using RStudio projects and git. Files are added to the staging area, and then committed to the local repository. You can push commits to the remote repository and pull new commits to your computer.
Introduce yourself to git
library(usethis) use_git_config(user.name ="Jane Doe", # Your name!user.email ="jane@example.org") # Your email (the one you use on GitHub)
Create an online repository
Go to github.com and log in.
On the top right corner, click on the “+” bottom and then “New repository”
Then:
Repository template: No template.
Repository name: myrepo or whatever you wish to name your new project.
Description: Any short description of the project. Write this for humans.
Public.
Initialize this repository with: nothing (we can set up everything from R).
Before going back to RStudio, copy the url for the repository. For example https://github.com/paocorrales/myrepo.git
Clone the repository locally.
File > New Project > Version Control > Git. In the “repository URL” paste the URL of your new GitHub repository https://github.com/paocorrales/myrepo.git.
Choose the folder where you want to create the project.
Choose “Open in new session”.
And Click on “Create Project”.
Commit local changes
Create a new RMarkdown file and save it.
Add it to the staging area and commit the file. You’ll need to add a descriptive message!
Make a change on the file, it can be anything. Save it.
Repeat step 2.
Now push!
Making remote changes
On the main repository page, click on the green bottom that says “Add a README”.
Add something to the file. READMEs usually are written in Markdown and contain information about the repo.
At the end of the page add a message on the first line and click on “Commit changes…”.
Come back to the main page to see the README.
Getting remote changes
Go back to RStudio.
To the the Git pane.
Click on the light blue arrow that says “Pull”.
Check the README file on the Files tab.
Working with others
Concep model of the remote workflow. A foreign remote repository can be forked to an owned remote repository with a “fork”. The remote repository is copied into a local repository with a “clone”. Files inside the local repository and the remote repository are synced with push and pull. The owned remote repository can be merged into the foreign remote repository with a pull request.