layout: true background-image: url(img/course-logo.png), url(img/logo_SBR.png), url(img//NTU-Logo-full-colour.png) background-position: right top 30px, left 50px bottom 50px,right 50px bottom 50px, top 350px left 500px background-size: 45%, 25%, 20% # Fundamentals of Data Science for EESS --- <br> <br> <br> <br> ## R session 03 - Git .font120[**Daniel Vaulot**] 2021-01-20 <br> <br> <br> --- layout: false class: middle, inverse # Outline .font150[ * Git * Commit and uncommit * Branching and merging * GitHub ] --- layout: true # Intro to Git --- background-image: url(img/Linus_Torvalds.jpg),url(img/git-logo.png) background-position: right 10px top 10px, right 50px top 300px background-size: 15%,10% ## 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`) ... --- .pull-left[ ## Resources * Pro GIT: https://git-scm.com/book/en/v2 * GitHub guide: https://guides.github.com * Happy Git with R: https://happygitwithr.com/ ## Software * Git: https://git-scm.com * Git desktop: https://github.com * GitHub account: https://desktop.github.com ] .pull-right[ <img src="img/Git-progit2.png" width="70%" style="display: block; margin: auto;" /> ] --- ## Version control system (VCS) - Traditional <img src="img/git-storage-traditional.png" width="60%" style="display: block; margin: auto;" /> <br> * Store data as changes to a base version of each file --- ## Version control system (VCS) - Git <img src="img/git-storage-git.png" width="60%" style="display: block; margin: auto;" /> <br> * Git takes a snapshot at each time * Only stores what has changed -- * Changes are stored in a **local database** inside your directory (.git) --- ## Three states for a file: committed, modified, and staged <img src="img/git-structure.png" width="30%" style="display: block; margin: auto;" /> * 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. --- layout: true # Local Git - Committing --- ## Three different ways of committing .font150[ * Using R studio interface * Using GitHub Desktop (my prefered) * Using Git bash - The hard way ] --- background-image: url(img/git-R-new-project.png), url(img/git-R.png) background-position: right 20px top 20px, right 20px top 200px background-size: 20%, 30% ## Create R project and initialize Git .student[ * 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 ] -- .student[ * On top-right window go Git tab * Stage the files you want to commit * Commit * Must write a message explaining what you changed ] --- background-image: url(img/git-R.png) background-position: right 20px top 20px background-size: 35% ## Commiting changes with R .student[ * Make a new change to the Rmd file * Stage and Commit * R shows the differences * Enter new comment and commit ] -- .student[ * Click on history * Each commit has a **hash** value * a80df29cc709e25db49cdfed11c0c1b0399d4a72 * Can open first version of the Rmd file ] --- background-image: url(img/git-github-desktop.png) background-position: right 20px top 20px background-size: 35% ## Commit with GitHub Desktop .student[ * Make a new change under R * Save * Open GitHub Desktop ] -- .student[ * File/New repository * Add existing repository * Navigate to directory ] -- .student[ * Commit change * You can check in R the Commit ] --- exclude: true background-image: url(img/git-bash.png) background-position: right 20px top 20px background-size: 35% ## Commit with Git bash .student[ * Make another change in the Rmd file * Go to Git tab * More / Shell... ] -- exclude:true .pull-left[ ```shell $ git status $ git add git-01.Rmd $ git status $ git commit ``` .student[ * Surprise, surprise you are in the VIM editor * Enter the commit message * ESC - : - x - ENTER * Check in GitHub Desktop ] ] --- layout: true # Local Git - Reverting --- background-image: url(img/git-github-desktop.png) background-position: right 20px top 20px background-size: 35% ## Revert Commit with GitHub Desktop .student[ * History * Right-click : Revert Commit ] -- .student[ * 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](#GitHub) --- exclude: true background-image: url(img/git-bash.png) background-position: right 20px top 20px background-size: 25% ## Revert Commit with Git bash .student[ * Find the hash of the commit you want to remove ] .pull-left[ ```shell $ git log $ git revert <hash> $ git revert a80df29cc709e25db49cdfed11c0c1b0399d4a72 ``` .student[ * Check in Rstudio the reversal ] ] --- layout: true # Local Git - Branching --- background-image: url(img/git-branch-08.png) background-position: center top 100px background-size: 70% ## Why create branches ? .font150[ * Testing: Do not destroy your own work * Collaborative work: Different people can contribute and fix bugs ] --- exclude: false ## How the data are stored <img src="img/git-commit-00.png" width="40%" style="display: block; margin: auto;" /> <br> Three elements: * **blobs** - contain the contents of the different files * **tree** - pointer to the list of the blobs * **commit** - pointer to the tree --- exclude: false ## Commit <img src="img/git-commit-01.png" width="60%" style="display: block; margin: auto;" /> --- exclude: false ## The master branch <img src="img/git-commit-02.png" width="60%" style="display: block; margin: auto;" /> <br> A branch in Git is simply a movable **pointer** to one commit. --- exclude: false ## Create a new branch <img src="img/git-branch-01.png" width="45%" style="display: block; margin: auto;" /> ```shell $ git branch testing ``` --- exclude: false ## Move the HEAD to the new branch <img src="img/git-branch-02.png" width="45%" style="display: block; margin: auto;" /> ```shell $ git checkout testing ``` --- exclude: false ## Make one commit on the testing branch <img src="img/git-branch-03.png" width="60%" style="display: block; margin: auto;" /> --- exclude: false ## Move the HEAD back to the master branch <img src="img/git-branch-04.png" width="60%" style="display: block; margin: auto;" /> ```shell $ git checkout master ``` --- exclude: false ## Do one commit on the master branch <img src="img/git-branch-05.png" width="50%" style="display: block; margin: auto;" /> <br> * Now we have two diverging branches --- exclude: false background-image: url(img/git-branch-06.png) background-position: right 20px top 20px background-size: 50% ## Merging .pull-left[ * Make sure you are on the master branch ```shell $ git checkout master ``` * Merging ```shell $ git merge testing Auto-merging README Merge made by the 'recursive' strategy. README | 1 + 1 file changed, 1 insertion(+) ``` ] --- exclude: false ## After merging <img src="img/git-branch-07.png" width="50%" style="display: block; margin: auto;" /> * Now can delete the branch `testing` <br> ```shell $ git branch -d testing ``` --- exclude: false background-image: url(img/git-R.png) background-position: right 20px top 20px background-size: 35% ## Let's do it using R studio Could be done also with GitHub Desktop or Git bash .student[ * Go to Git tab * Create a new branch `test`or the name you want ] -- exclude: false * Notice that R creates the branch and switch to the branch -- exclude: false .student[ * 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` ] -- exclude: false * Observe how the Rmd file changes back and forth... -- exclude: false .student[ * Go to branch `master` * Make some change in the Rmd file (elsewhere e.g. Plot) and save * While on branch `master`, commit ] --- exclude: false background-image: url(img/git-github-desktop.png) background-position: right 20px top 20px background-size: 35% ## Merge back Could be done also with Git bash .student[ * Make sure you are on branch `master` * Go to GitHub Desktop * Branch / Merge into Current Branch * Select the `master` ] -- exclude: false .student[ * Branch/Delete * Choose `test` * Look at history ] * There can be conflicts * You need to edit file to solve the conflict --- layout: true # GitHub --- name: GitHub ## Upload your project to GitHub .student[ * Go to GitHub Desktop * Publish to Github * Give a name ] <img src="img/git-github-publish.png" width="80%" style="display: block; margin: auto;" /> --- ## GitHub .student[ * Go to your GitHub account * You should have a new repository ] <img src="img/git-github-web.png" width="50%" style="display: block; margin: auto;" /> --- background-image: url(img/git-github-web-02.png) background-position: right 20px top 20px background-size: 50% ## Edit on GitHub .student[ * Click on file Rmd * Edit * Commit ] --- background-image: url(img/git-push-pull.png) background-position: right 20px top 100px background-size: 70% ## 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 --- background-image: url(img/git-pull.png) background-position: right 20px top 20px background-size: 60% ## Pull from server .student[ * 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 --- background-image: url(img/git-pull-request.png) background-position: center right 30px background-size: 55% ## Cloning a project from GitHub <img src="img/git-clone.png" width="55%" style="display: block; margin: auto;" /> --- exclude: true background-image: url(img/git-pull-request.png) background-position: center right 30px background-size: 55% ## Pull request * You work on a project with someone else * You cloned the project into your computer * You created a new branch and made some change * You want to merge back into the project -- exclude: true .student[ * R * Create a new branch `test2` and switch * Make some changes * Save and commit * GitHub Desktop * Push changes to GitHub * Branch / Create a Pull Request * GitHub * Validate the Changes and Merge into `master` * Delete the branch `test2` ] --- layout: false class: inverse middle # Recap .font150[ * Save and Commit * Push and Pull * Branching and merging (use only in multiuser projects) ]