ETC5513: Collaborative and Reproducible Practices
Tutorial 3
🎯 Objectives
- Get familiar with the command line interface
- Set up
git
on your local machine - Configure
git
- Clone your first repository
- Modify your first repository with the command line interface
- Learn
git
flow using RStudio
Exercise 1: Practice with the terminal
Using your command line terminal:
- Check the directory where you currently are
- Navigate to your Desktop (using
cd
andls
until you arrive at your destination).
- List of files and directories you have on your desktop
- Create a folder called
ETC5513_tutorial3
.
Hint:mkdir
Please complete the next points in this exercise after the tutorial and before you look at the solutions:
- Navigate into
ETC5513_tutorial3
and inside that folder create another folder calledTutorial3
- Inside
ETC5513_tutorial3
create two folders at the same time calledData
andFigures
- Use the command
touch
to create two files inside Tutorial3example.qmd
example1.qmd
- Delete
example1.qmd
- Inside
ETC5513_tutorial3
create a new file calledfile1.txt
- Move
file1.txt
fromETC5513_tutorial3
into a new folder (or directory) calledExample
. The folderExample
andETC5513_tutorial3
should be at the same level.
Hint: mv
Exercise 2: Git and GitHub configuration
1. Git configuration
Open your command line interface/Terminal/Git Bash Shell window and type the following:
Please replace “Firstname lastname” with your own details and choose a name that is identifiable for credit (this information is the one that identifies you)
git config --global user.name "Firstname lastname"
Set your Monash email address to be associated with each history commit:
git config --global user.email "valid email address"
Now go to your Rstudio session and follow the next steps:
- Select Tools from the menu and go to Global Options
- Click Git/SVN on the left panel
- Click Enable version control interface for RStudio projects at the top and continue with the steps below.
Next we are going to create an SSH key as follows (we want to do this to avoid having to input our GitHub credentials each time we interact with GitHub):
- Click on Create SSH Key and another pop-up prompt will appear where you will be asked to provide a PassPhrase. You do not need a passphrase. It is considered more secure but be aware you will have to enter this phrase as a password every time when the key is used.
- Click View public key and copy the key to the clipboard (ctrl c (Windows) or command c (mac)).
- Navigate to GitHub and go to the top right corner where your avatar is, select your avatar and select Settings from the drop-down menu.
- Go to SSH and GPG keys item in the left side menu inside your GitHub account. Click the green “New SSH key” button and give your key a title and paste the key you copy from Rstudio in the step above into the Key text-box. Then click “Add SSH key”.
The process described above allows you to interact between your local Git and GitHub without having to write your GitHub logging details each time you do a git operation between your local and your remote repository.
2. Creating a repository on GitHub
Using your internet browser:
- Log in into your GitHub account following this link
- Create a new private repo called Tutorial3 and make sure the repo is initialized with a README.md file by ticking on the option “Initialize this repository with a README” at the bottom of the page.
- Click the name Tutorial3 to look inside your GitHub project, identify what is the HTTPS and SSH address of this project by looking at the green tab (code).
See below the solutions:
3. Cloning and modifying our first GitHub repo using our Terminal/Command line interface/Git Bash shell and with Rstudio
Using your internet browser:
- Clone the GitHub repository that you created in the previous exercise in your computer using the command line interface/Terminal/Git Bash shell. Remember that it is very important that you first decide where you want to clone the project in your computer and you navigate to that location using the command line interface/Terminal/Git Bash shell.
The git command to clone a GitHub repo is git clone SSH-repo GitHub path
(SSH-repo GitHub path refers to the path that you will fine in your GitHub project page under Code > SSH and copy the address that you see, it will end in .git. Mine looks like this
git@github.com:ETC5513demo/Tutorial3.git
Then proceed to clone the repository in your local machine using the command line interface/Terminal/Git Bash shell:
git clone git@github.com:github.com/ETC5513demo/Tutorial3.git
(replace the SSH with your own!)
(you can obtain this from within your GitHub project –> code)
You will be prompted to input your GitHub log in details (only for this time). Enter your GitHub user name and password
Open your Rstudio and create a New Project associated with the folder of the GitHub repository that you just cloned in step 1:
Projects > Create a New project > From an existing directory
From the bottom left pane in your Rstudio open the README.md file and add the following sentence:
“This repository is an example about how to integrate GitHub with Rstudio”
In your command line interface/Terminal/Git Bash shell type
git status
and observe that the file has been marked as modified.Add this file to the staging area, create a commit and push the changes to the remote repository using our command line interface/Terminal/Git Bash shell.
Hints:git add filename
;git commit -m "Commit message"
;git push origin main
. Remember that you can use git status at anytime during this process to see the status of your git repository. In summary:
- Inside your README file create a new section as follows: “This is a section”. Observe in the top right pane of your Rstudio how the status for the file README.md in the staging is changed to M after you modified the file in the top right pane of your Rstudio window.
- Now we are going to do a commit using Rstudio: Select the check box next to the file README.md in the staging area within your Rstudio and click:
- Commit
- Write a message in the commit window “This is my first commit” in the window that pops up
- Commit
- Push (you will be prompt to insert your GitHub credentials one last time. After that you will be able to commit and push without having to insert your GitHub credentials! )
Open your GitHub, go to Tutorial3 project and make sure your README.md file has been updated.
Look at the commits history.
After the tutorial:
Use your Terminal/command line interface/Gitbash to interact between your local repository and your remote repository
(Please complete this exercise at your pace after the tutorial and before you look at the solutions!!).
- Continue working in the Rstudio Project that you created in the previous exercise. Inside your Rstudio project create a new Quarto file (File> New File > Quarto document) and call it Tutorial3.qmd. If you are prompted to install some R packages say yes!)
Rstudio will create a qmd file with some code inside that produces a plot. You don’t need to modify that.
- We are going to include some data into our Data folder:
- Add a new section in Tutorial3.qmd with title: Write data from R into a csv file
- Create a new R code chunk and type inside
write.csv(cars, file = "Data/cars.csv")
. Press Render. This should create a csv file called cars inside your Data folder.
- Now we are going to use your Terminal/command line interface/Gitbash to commit the changes using the directly. Please type the following in your Terminal/command line interface/Gitbash:
git add .
(orgit add *
to commit all the changes)git commit -m "Adding folder Data"
git push origin master
- General workflow to interact with GitHub using the Terminal:
git clone
(A repository is typically clone only one time from the remote repository)git pull
(The git pull command is used to fetch and download content from a remote repository and immediately update the local repository to match that content. You should do this every time you star working in your local repository)git status
(The git status command displays the state of the working directory and the staging area)git add <file_name>
(The git add command adds a change in the working directory to the staging area)git add *
(Will commit all the changes)git commit -m "Message"
(m = message for commit. The git commit is used to create a snapshot of the staged changes along a timeline of a Git projects history.)git push origin <branch_name>
(The git push command is used to upload local repository content to a remote repository. )