ETC5513: Collaborative and Reproducible Practices
Tutorial 4
🎯 Objectives
- Create repositories and syncrhonize them between your local and remote repos
- Create a Quarto report with a tabell, figures and sections that are labelled and referenced
- Create branches in local and remote repositories
- Deal with merge conflicts
Exercise 1: Create a new repository (repo) in GitHub
Log in into your GitHub account following this link.
Create a new repo called Tutorial4 and initialize it with a
README.md
file.
- Clone this repository in your local computer. Please make sure that you have Git and GitHub configured in your computer. You should have done this in your Week 3 tutorial.
Open in Rstudio the file
README.md
, delete any text that is inside the file. Usingmarkdown
language please add a heading with title “This is tutorial 4”.Using your command line interface/Terminal or Git Bash Shell, Stage, commit and push the changes to the remote repository into GitHub. Do not use the Terminal inside Rstudio!
In the same file
README.md
inside the heading that you created in (5) add the following sentence “In this tutorial we are learning to create and merge branches as well as including the untracked files into our local repo”.Stage, commit and push the changes to the remote repository in GitHub (this time you shouldn’t have to input your GitHub login details).
- Go to GitHub and confirm that you have three commits in your repo history.
- Create a file called
Tutorial4.qmd
that contains the following and install all the required packages:- Title: Learning to reference figures, tables and sections
- Author: Write your name
- Date information
- The file will render to html
- Include the following in the YAML (and delete any other constant that might be in the file):
- Using your command line interface/Terminal or Git Bash Shell, stage, commit and push the changes to the remote repo in GitHub.
- Go to GitHub and inspect your commits in the remote repo.
Create a new branch in our local repository
Continue working in the same project as in the previous exercise.
Use the Terminal create a new branch. Please make sure you are in main.
Hint:git branch
- Follow the workflow that you learned during the lecture to create a new branch using the command line interface/terminal. Call the new branch
Feature
. You should now be on branchFeature
. hint: Usegit checkout -b branchname
and/orgit branch
andgit checkout
.
Refresh your Rstudio to clearly reflect in which branch you are currently working.
Move from this new branch called
Feature
tomain
and back toFeature
.
- Move to
Feature
and deleteFeature
from your local repository. Is this branch local or it is remote?
- You cannot be in the branch that you want to delete. Move into the
main
branch and then deleteFeature
.
- Create
Feature
locally again, move into that branch and push the branch into the remote repo in GitHub.
Hint:
git add .
(like this we are adding all the modified files into the staging area)git commit -m "Updating Feature"
git push origin Feature
- Double check in GitHub to see that the branch is now also created in the remote repo. You should now have two branches.
Go back to your Rstudio, open the
README.md
file and add a new sentence “We are going to create branches using the terminal”.Using the terminal add these changes into the remote repository as follows:
git add .
(like this we are adding all the modified files into the staging area)git commit -m "Updating Feature"
git push origin Feature
- Go back to
main
, openTutorial4.qmd
and create global options for the figures in the file so that the figures in your report are aligned in the center. Save the changes in the file.
- Insert a new R code chuck and load the library
tidyverse
.]
- Continue editing
Tutorial4.Rmd
and now create a new heading with title: Learning to reference figures.
- Inside an R chunk called Figref create 100 values generated from a normal distribution and store them in a variable called
x
. Create a sequence of values going from 1 to 100 and store them iny
. The create a data frame and produce a line plot as follows:
- Continue working on
Tutorial4.qmd
. Add a caption inside the R code chuck saying “This is random noise”.
- Write a sentence after the plot and reference this figure.
Add a new heading with title “Learning to reference tables”.
Create a table using the first 5 lines of the data frame
dat
.
- Add in a sentence in the text and reference the table.
- Using the command line interface/terminal/Git Bash Shell, stage, commit and push the changes to
main
:
- Refresh Rstudio and go to the diff window. There, select history and display all branches. Your
main
branch should now be ahead of yourFeature
branch in the diagram:
- Using the terminal you can merge your
Feature
branch withmain
branch. Inspect in the diff window if both branches are now merged. Remember you need to move to the branch where we want to include the changes, in this casemain
:
Hint: git merge Feature -m "Merging branches"
Go and inspect the diff window in Rtudio. Now both branches should be merged.
To do at home: You can also create and changing branches directly using the Rstudio GUI interface. Repeat this exercise at home using Rstudio instead of the terminal. When you create the branch as follows the branch gets also updated in the remote repo in GitHub.
Branching and merging conflicts
- Your GitHub repository should contain the files
README.md
andTutorial4.qmd
. - Check the history of your commits and check that your branch is merged to main branch.
- Create a conflict: Inside Rstudio open the
README.md
file and change the first heading “This is tutorial 4” to “I am creating a conflict” - Create a commit for this change with message “Conflict1”. DO NOT PUSH the commit yet.
- Select the changes you want to commit and click stage selection. Add a commit message and click Commit. Do NOT push (yet).
- Update your
README.md
on GitHub directly. At the same location in theREADME.md
where you wrote the sentence “I am creating a conflict part 2” in your local repo, now add the following sentence “I have two conflicting versions”.
- Now go to Rstudio and push your commit (you can simple click on Push on the top left pane - You don’t need to click to commit cause you have already done it before.
- You will get an error message as follows.
- Open README.md in RStudio and fix the conflicts: Remove both sentences and add a new one instead that says “I have learned to create and fix a conflict!”.
- Commit the changes.
- Push the repo to GitHub.
Extra exercise
- Go over the referencing project that was covered in the lecture this week (you can find it in the Moodle site).
- Make sure you fully understand how to reference figures, tables and sections by going over the qmd file.
- You might create a new GitHub repository and add this project there so that you can keep practicing what it was covered in the tutorial today!