Using Git and Github
April 22, 2025
Compiled software are a bunch of files. At each software improvment, developers want the software to be stable with no regression. For that, they have to know what files have changed between snapshots.
add & commitadd & commitadd & commitadd & commitadd & commitadd & commitbranchesbranchesmain par défault): c’est un série de commentaires (commit)commit) est ce que l’on appelle la tête de la branche (HEAD), elle contient la version la plus à jour des fichiers.commit) est attachée une version des fichiers.local vs remotegit init)git add)git commit)git fetch & git push)git fetch & git pull)git —versiongit configFor more details: https://happygitwithr.com/install-git
In order to reduce the learning curve, we will learn the R Studio way of interacting with git
So many other ways to interact with git:
gert packageAll the key concepts and process still the same!
To use git in Rstudio, you need to create a new Project. Let’s start by creating our project directory.
Open RStudio, in the top right corner create a new project
You can now look under the git tab, you should see this:
Create a new file: go to the menu File => New file => R Script. Make sure to save it with the name script_1.R.
What happens in the git tab?
This is as simple as “checking off” the file in the Git tab. This is called Staging the file.
What is a gitignore? A gitignore is a file that lists the file you never want git to track. It can match certain file names (for instance, .csv or .tif files). This can be useful in case you need to make sure certain files (like data files or large files), do not get added.
Click on commit on the top of the file list. This window should appear:
Before committing anything you need to add a commit message. It is important to add a useful message to your commit, a bit like a journal entry, so that you can remember what you committed.
Click on commit in order to commit the changes!
Let’s make sure you have the following packages installed on your computer:
usethis to create a GitHub tokengitcreds to store the token in your local systemIn the first workshop, we learned the basics of Git. We covered:
.gitignore file (relevant for sensitive data)git Workflow Overview ReminderThe typical workflow:
Tip
We can make an analogy with taking a family picture, where each family member would represent a file.
Staging files is like deciding which family member(s) are going to be on your next picture Committing is like taking the picture
Step 1. For those who don’t have an account, sign up on GitHub using this link
We have to store a token in our local system. This token is used to authenticate your computer with Github.
Tip
Token = password
Run the following command in RStudio
ghp_Run the following command in RStudio
This command will open a prompt in RStudio
Paste the token you copied in the previous step
Assess if everything is fine and the credentials are OK
Run the following command in RStudio
Note
This way, you will not have to enter your credentials every time you push or pull from GitHub. If you want to know more on this authentification process, have a look at the usethis documentation
Run the following command in RStudio
Note
usethis::use_github(private = TRUE) if you want your repository to be private, because you are working with sensitive data
The configuration is done and the local is now sync! Lets have a quick look at the Github repo on the website.
Lets go back on RStudio and check the Git tab. Two new buttons Pull and Push are now available.
Commit, move a changed local file to your local staging areaPull, will get the latest remote version and try to merge it with your local versionPush, will send your local version to the remote version of the repository (in our case GitHub)You have to repeat step 2-4, every time you edit, add, delete a new file if you want to keep your local and remote repository in sync.
If I’m a collaborator on a project, I need to be able to get the latest version of the code from the remote repository.
To accomplish this, I Clone the remote repository to my local computer. Clone means to copy the entire contents of a GitHub repository to your local computer.
Navigate to the repository on GitHub and click on Code. Select HTTPS and copy the link.
Clone, copy the entire contents of a GitHub repository to your local computer (done once per computer)Commit, move a changed local file to your local staging areaPull, get file(s) from the remote to your local computer – opposite of a “push”Push, move file(s) to the remote from your local computer – opposite of a “pull”20:00
Tip
Reminder on git file status:
Invite a collaborator to your repository on GitHub.
git conflictsgit conflicts on RStudiogit conflicts on RStudiogit conflictsIn teams of 2, edit the same file at the same time in the same repository. Then, try to push your changes to the remote repository. A conflict should occur, let’s try to resolve it.
15:00
A lot of the content of this workshop come from or are based on the following resources:
Quarto is a tool for creating documents, presentations, websites, and more from plain text files.
File > New File > Quarto Document05:00
Tip
In Rstudio, you can use the Visual mode to edit your Quarto document. This mode allows you to see the document as it will be rendered, while still being able to edit the source code.
Tip
You can render the document in a viewer Rstudio panel instead of opening a new window in the browser.
---).Qmd filekey: value (YAML syntax)It depends on the output format. Options are available in the Quarto documentation:
# Heading 1## Heading 2### Heading 3#### Heading 4##### Heading 5###### Heading 6Normal text_Italic_ *text*__Bold__ **text*****Bold italic*** **_text_**~~Strikethrough text~~> This is a blockquote$e^{\pi i} + 1 = 0$Insert a image using the following syntax:
Insert a link using the following syntax:
05:00
Question: Where is the code chunk and where is the markdown text?
```{r}
#| label: chunk-options
#| fig-cap: "Comparison on Sepal Length and Sepal Width in the iris dataset"
#| echo: true
library(ggplot2)
data(iris)
ggplot(
data=iris,
aes(x = Sepal.Length,
y = Sepal.Width)
) +
geom_point(
aes(color=Species, shape=Species)
) +
xlab("Sepal Length") +
ylab("Sepal Width") +
ggtitle("Sepal Length-Width")
```| Option | Description |
|---|---|
eval |
Evaluate the code chunk (if false, just echos the code into the output). |
echo |
Include the source code in output. |
output |
Include the results of executing the code in the output (true, false, or asis to indicate that the output is raw markdown and should not have any of Quarto’s standard enclosing markdown). |
warning |
Include warnings in the output. |
error |
Include errors in the output (note that this implies that errors executing code will not halt processing of the document). |
include |
Catch-all for preventing any output (code or results) from being included. |
It is also possible to include inline code in the text.
Dans le dataset iris, il y a une taille d'échantillonnage
de `{r} nrow(iris)` individus pour l'ensemble des espèces.Which gives:
Dans le dataset iris, il y a une taille d’échantillonnage de 150 individus pour l’ensemble des espèces.
ggplot2 packageSepal.Length variable in the iris dataset.15:00
To do so, we have to transform our Quarto document into Quarto project. A Quarto project is a directory that contains a _quarto.yml file.
We have to render the Quarto document to HTML. This will create a docs folder in the project directory, as specified in the _quarto.yml file.
This folder will contain the HTML files of the Quarto document. Then we commit and push the changes to the remote repository.
Finally, configure your GitHub repository to publish from the docs directory of your main branch: