+ - 0:00:00
Notes for current slide
Notes for next slide

Fundamentals of Data Science for EESS





R session 03 - Git

Daniel Vaulot

2021-01-20




1 / 32

Outline

  • Git
  • Commit and uncommit
  • Branching and merging
  • GitHub
2 / 32

Intro to Git

What is Git ?

  • VCS: Version control system
  • Created by Linus Torvald in 2005
3 / 32

Intro to Git

What is Git ?

  • VCS: Version control system
  • Created by Linus Torvald in 2005

What Git useful for ?

3 / 32

Intro to Git

What is Git ?

  • VCS: Version control system
  • Created by Linus Torvald in 2005

What Git useful for ?

  • Keep traces of all changes done to your work
    • Undo changes
    • Perform test on software you are developing
    • Backup your work (GitHub)
  • Team work
    • Collaborate with a small team (GitHub)
    • Participate into software development (GitHub)
    • Write paper in collaborative way (Overleaf - On-line Latex)
    • Provide supplementary data for your papers (Figshare) ...
3 / 32

Intro to Git

Resources

Software

4 / 32

Intro to Git

Version control system (VCS) - Traditional


  • Store data as changes to a base version of each file
5 / 32

Intro to Git

Version control system (VCS) - Git


  • Git takes a snapshot at each time
  • Only stores what has changed
6 / 32

Intro to Git

Version control system (VCS) - Git


  • Git takes a snapshot at each time
  • Only stores what has changed
  • Changes are stored in a local database inside your directory (.git)
6 / 32

Intro to Git

Three states for a file: committed, modified, and staged

  • Need to be familiar with the vocabulary
7 / 32

Intro to Git

Three states for a file: committed, modified, and staged

  • Need to be familiar with the vocabulary
  • Committed: data is safely stored in your local database.
7 / 32

Intro to Git

Three states for a file: committed, modified, and staged

  • Need to be familiar with the vocabulary
  • Committed: data is safely stored in your local database.
  • Modified: file changed BUT not committed in your database yet.
7 / 32

Intro to Git

Three states for a file: committed, modified, and staged

  • Need to be familiar with the vocabulary
  • Committed: data is safely stored in your local database.
  • Modified: file changed BUT not committed in your database yet.
  • Staged: modified file is marked to go into your next commit snapshot.
7 / 32

Local Git - Committing

Three different ways of committing

  • Using R studio interface
  • Using GitHub Desktop (my prefered)
  • Using Git bash - The hard way
8 / 32

Local Git - Committing

Create R project and initialize Git

  • Create project in a new directory : git_01
  • Create a new project
  • Select "Create git repository"
  • You can also change Project Options (Tools/Project Options/Git/Git)
  • Create a new Rmd file and save it
9 / 32

Local Git - Committing

Create R project and initialize Git

  • Create project in a new directory : git_01
  • Create a new project
  • Select "Create git repository"
  • You can also change Project Options (Tools/Project Options/Git/Git)
  • Create a new Rmd file and save it
  • On top-right window go Git tab
  • Stage the files you want to commit
  • Commit
    • Must write a message explaining what you changed
9 / 32

Local Git - Committing

Commiting changes with R

  • Make a new change to the Rmd file
  • Stage and Commit
  • R shows the differences
  • Enter new comment and commit
10 / 32

Local Git - Committing

Commiting changes with R

  • Make a new change to the Rmd file
  • Stage and Commit
  • R shows the differences
  • Enter new comment and commit
  • Click on history
  • Each commit has a hash value
    • a80df29cc709e25db49cdfed11c0c1b0399d4a72
  • Can open first version of the Rmd file
10 / 32

Local Git - Committing

Commit with GitHub Desktop

  • Make a new change under R
  • Save
  • Open GitHub Desktop
11 / 32

Local Git - Committing

Commit with GitHub Desktop

  • Make a new change under R
  • Save
  • Open GitHub Desktop
  • File/New repository
  • Add existing repository
  • Navigate to directory
11 / 32

Local Git - Committing

Commit with GitHub Desktop

  • Make a new change under R
  • Save
  • Open GitHub Desktop
  • File/New repository
  • Add existing repository
  • Navigate to directory
  • Commit change
  • You can check in R the Commit
11 / 32

Local Git - Reverting

Revert Commit with GitHub Desktop

  • History
  • Right-click : Revert Commit
12 / 32

Local Git - Reverting

Revert Commit with GitHub Desktop

  • History
  • Right-click : Revert Commit
  • Go back to R
  • Magic the changes have been reverted...
  • Examine history (refresh if necessary)
  • In fact revert is a new commit
12 / 32

Local Git - Reverting

Revert Commit with GitHub Desktop

  • History
  • Right-click : Revert Commit
  • Go back to R
  • Magic the changes have been reverted...
  • Examine history (refresh if necessary)
  • In fact revert is a new commit

Next slide: GitHub

12 / 32

Local Git - Branching

Why create branches ?

  • Testing: Do not destroy your own work
  • Collaborative work: Different people can contribute and fix bugs
13 / 32

Local Git - Branching

How the data are stored


Three elements:

  • blobs - contain the contents of the different files
  • tree - pointer to the list of the blobs
  • commit - pointer to the tree
14 / 32

Local Git - Branching

Commit

15 / 32

Local Git - Branching

The master branch


A branch in Git is simply a movable pointer to one commit.

16 / 32

Local Git - Branching

Create a new branch

$ git branch testing
17 / 32

Local Git - Branching

Move the HEAD to the new branch

$ git checkout testing
18 / 32

Local Git - Branching

Make one commit on the testing branch

19 / 32

Local Git - Branching

Move the HEAD back to the master branch

$ git checkout master
20 / 32

Local Git - Branching

Do one commit on the master branch


  • Now we have two diverging branches
21 / 32

Local Git - Branching

Merging

  • Make sure you are on the master branch
$ git checkout master
  • Merging
$ git merge testing
Auto-merging README
Merge made by the 'recursive' strategy.
README | 1 +
1 file changed, 1 insertion(+)
22 / 32

Local Git - Branching

After merging

  • Now can delete the branch testing
$ git branch -d testing
23 / 32

Local Git - Branching

Let's do it using R studio

Could be done also with GitHub Desktop or Git bash

  • Go to Git tab
  • Create a new branch testor the name you want
24 / 32

Local Git - Branching

Let's do it using R studio

Could be done also with GitHub Desktop or Git bash

  • Go to Git tab
  • Create a new branch testor the name you want
  • Notice that R creates the branch and switch to the branch
24 / 32

Local Git - Branching

Let's do it using R studio

Could be done also with GitHub Desktop or Git bash

  • Go to Git tab
  • Create a new branch testor the name you want
* Notice that R creates the branch and switch to the branch
  • Make some change in the Rmd file and save
  • While on branch test, commit
  • Go back and forth between the two branches master and test
24 / 32

Local Git - Branching

Let's do it using R studio

Could be done also with GitHub Desktop or Git bash

  • Go to Git tab
  • Create a new branch testor the name you want
  • Notice that R creates the branch and switch to the branch
  • Make some change in the Rmd file and save
  • While on branch test, commit
  • Go back and forth between the two branches master and test
  • Observe how the Rmd file changes back and forth...
24 / 32

Local Git - Branching

Let's do it using R studio

Could be done also with GitHub Desktop or Git bash

  • Go to Git tab
  • Create a new branch testor the name you want
* Notice that R creates the branch and switch to the branch
  • Make some change in the Rmd file and save
  • While on branch test, commit
  • Go back and forth between the two branches master and test
* Observe how the Rmd file changes back and forth...
  • Go to branch master
  • Make some change in the Rmd file (elsewhere e.g. Plot) and save
  • While on branch master, commit
24 / 32

Local Git - Branching

Merge back

Could be done also with Git bash

  • Make sure you are on branch master
  • Go to GitHub Desktop
  • Branch / Merge into Current Branch
  • Select the master
25 / 32

Local Git - Branching

Merge back

Could be done also with Git bash

  • Make sure you are on branch master
  • Go to GitHub Desktop
  • Branch / Merge into Current Branch
  • Select the master
  • Branch/Delete
  • Choose test
  • Look at history
  • There can be conflicts
    • You need to edit file to solve the conflict
25 / 32

GitHub

Upload your project to GitHub

  • Go to GitHub Desktop
  • Publish to Github
  • Give a name

26 / 32

GitHub

GitHub

  • Go to your GitHub account
  • You should have a new repository

27 / 32

GitHub

Edit on GitHub

  • Click on file Rmd
  • Edit
  • Commit
28 / 32

GitHub

Computer <--> Server synchronization

  • How are we going to synchronize these changes back to our computer ?

  • Pull: from server to computer

  • Push: from computer to server
29 / 32

GitHub

Pull from server

  • Go to GitHub desktop
  • Fetch
  • Pull
  • Go to RStudio - What happened to your Rmd file
  • If you make commits locally you need to push to server
  • You can push several commits at once
30 / 32

GitHub

Cloning a project from GitHub

31 / 32

Recap

  • Save and Commit
  • Push and Pull
  • Branching and merging (use only in multiuser projects)
32 / 32

Outline

  • Git
  • Commit and uncommit
  • Branching and merging
  • GitHub
2 / 32
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow