lecture14: Git and the magic of version control

Prashant K

2024-03-05

Announcements

Midterm exam

  • You can get help on the midterm exam in office hours/discussion board etc. please make use of it

    • Please make sure you are on the latest version: Change log:

      • 2/Mar/24: 1 pm. Updated points to 60 total (previously 55) adding questions 1e2, 1f, 4.d

      • 2/Mar/24: 7 pm. Fixed rendering to generate a standalone html file, clarified format

  • HW4 rubric is on Canvas/files now!

Today’s class

  • Why share code with others?

  • What is version control and why do we need it?

  • How does git do version control?

    • git vs (github.com / gitlab.com / ..)
  • Live practice: Setup/use git through the command line and Rstudio interface

Sharing code for reproduciblity

  • Sharing source code is useful

    • Sharing your data analysis in a paper

    • Getting someone to look at your code and help

  • Why not just share on Google drive/dropbox then?

    • Github interfaces very well with Git. Makes it quicker to setup and run your code

What is a version? why keep track?

Many many writing drafts

Synchronizing changes in Midterm/assignment/worksheets easily!

Easy to see changes too

How does git work?

the three main sections of a Git project: the working tree, the staging area, and the Git directory

Working tree, staging area, and Git directory

Working tree, staging area, and Git directory

Task1 : Setting up and git basics on commandline

  • Find a terminal : regular or console within Rstudio. We shall use the Rstudio one to demo
    • git init: initiate a repository

      • Use this command to make a git repository in the same directory as your .Rproj file. check that that the working directory of the console matches before using these commands below
      • What is the branch name that you see?
      • We want to change that to main using git branch -m main (-m is short for --move)
      • To change the default branch name use git config --global init.defaultBranch main
    • For the first time when using git, you have to introduce your name and email ID using these commands git config –global user.name ".." and same for user.email ".."

    • touch example.txt : make a text file to practice on

    • git add: stage files ; git commit -m "message" : commit files with a short message

    • add 3 lines of text : purpose: .. ; author: ; date:

    • git diff to see what changed

    • again, do: git add: stage files ; git commit -m "message" : commit files with a short message

      • Shortcut git commit -am ".." adds all changed files without needing to mention explicitly (-a = all)

Task2 : Connecting git with Rstudio for GUI access

  • Convert the text file to a qmd markdown file! git mv example.txt example.qmd : then open in Rstudio and bold the keywords
    • Explanation: mv command stands for move file or directory. When using git mv, git registers it as “a file renaming change” rather than”an old file deleted and a new one made
  • Now use Rstudio’s git integration to visually see changes, and commit!
  • Add a code chunk and one line of R code, and commit again

Task3: Connecting to a remote website for sharing/synchronization across computers

  • See the github organization for this class and repositories - explain private repo vs public
  • Git clone git_practice repo (already put in a simple .qmd file here)
  • Solve the ggplot question-1 and commit changes
  • Watch me make changes to the file and push it
  • Do a git pull : to bring fresh changes I made
  • Solve question-2
  • commit and push using only Rstudio now

More things we won’t cover

  • Try doing a: git push: where does this push to? Remember, you don’t have edit access to public repositories unless you are added as a collaborator
  • Make a fork on github.com ; note the url of your forked repository (different from the original)
    • I recommend using the SSH method. You need to setup SSH keys for your particular computer by following this guide in chapter 10
  • Change remote url with git remote set-url origin ..url here..
  • Push changes to your own repo fork online using git push -u origin main

Summary

Last class

  • Why share code with others?

  • What is version control and why do we need it?

  • Initializing git and command line

Today’s class

  • Learnt how to git add and git commit from the command line interface (CLI)

  • Learnt the same with a more intuitive graphical interface (GUI) within Rstudio

  • Using git pull to grab public code from github.com