Biostatistical Computing, PHC 6068

GitHub

Instructor: Zhiguang Huo (Caleb)
Guest Speaker: Shangchen Song

Monday October 4, 2021

Outline

Git

Benefits of version control

Setting up the Git

git config --global user.name "Sam Smith"
git config --global user.email sam@example.com
git config --list

Git basics

Git workflow

git add XXX
git commit -m "your message"

Example to make change in git repository (use R codes as example)

WD <- '~/Desktop'
setwd(WD)

usethis::create_package("GatorPKG", open = FALSE) ## open = FALSE will prevent R open a new R studio session.
WD2 <- '~/Desktop/GatorPKG'
setwd(WD2)
WD <- 'C:/Users/ss/Desktop/'
setwd(WD)
usethis::create_package("GatorPKG", open = FALSE) ## open = FALSE will prevent R open a new R studio session.
##' Add up two numbers (Description)
##'
##' We want to add up two numbers, blalala... (Details)
##' @title add two numbers
##' @param x first number
##' @param y second number
##' @return sum of two numbers
##' @author Caleb
##' @export
##' @examples
##' f(1,2)
f <- function(x, y) x + y

Example to make change in git repository (git part)

cd ~/Desktop/GatorPKG
git init
git status
git add -A ## -A stage all your files
git status
git commit -m "first commit"
git status

git status

  1. before staging your workspace

  1. after staging your workspace, before committing

  1. after committing

git add

git add newFile
git add newFile1 newFile2 newFile3
git add -A

git commit

git commit -m "any message you want to make here"
git log

Other files in the repository

ls -a ## list all files, including hidden ones

More on git

So far, local version control

Distributed version control

Distributed version control

Connect the local repository with GitHub

Set up GitHub and connect with your local repository (1)

Set up GitHub and connect with your local repository (2)

cd "~/Desktop"
git clone https://github.com/lovestat/GatorPKG.git

Make sure to use this way to initialize your repository, in order to connect to GitHub

Set up GitHub and connect with your local repository (3)

Put your code in the GatorPKG package

Set up GitHub and connect with your local repository (4)

devtools::document()
git add -A
git commit -m "1st R pacakge"
git log

You will see a SHA1 number (Secure Hash Algorithm 1), this is the access number of a commit object

git push
devtools::install_github("lovestat/GatorPKG")
library(GatorPKG)
f(1,2)
?f

Your turn

Goals:

Add README.md on GitHub

# testGatorPKG
This is a fancy R package

## how to install the R package
`devtools::install_github("lovestat/GatorPKG")`

## example
`f(1,2)`

More on the remote

Two ways to get your PC updated with the latest remote repository

Keep updated with the remote

git fetch
git pull

Clone an existing remote repository to your local PC

git clone https://github.com/cran/mclust.git

After some changes of this package

Fork a repository on your GitHub account

Whole picture for Git operations

Git branches

Basic commands for Branching

git checkout -b <branchname>
git checkout <branchname>
git branch
git branch -d <branchname>
git push origin <branchname>

Exercise for creating an additional branch

git checkout -b vignette
usethis::use_vignette("GatorPKG")
git add -A 
git commit -m "include vignette"
git branch
git checkout main

Git revert

Go back to the previous snapshot by SHA1 number

git checkout -b old-state f0d8506
git branch
git checkout main

Git merge (when there are no conflicts)

git checkout main
git merge --no-ff vignette
git log

Compare with another commit object

git diff
git diff --base <filename>
git diff aSHA1 bSHA1
git diff <sourcebranch> <targetbranch>

Set alias for github command

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --"
git lg

Add R CMD check on GitHub

usethis::use_github_action_check_standard()

Add README badges/icons for your package

usethis::use_cran_badge()
usethis::use_lifecycle_badge("stable")
use_github_actions_badge(name = "R-CMD-check", repo_spec = NULL)

Build A package website for your package (1)

usethis::use_pkgdown()
pkgdown::build_site()
usethis::use_github_action("pkgdown")
git add -A
git commit -m "add pkgdown"
git push

Build A package website for your package (2)

Display html on github

Use GitHub as a raw code repository

address <- "https://raw.githubusercontent.com/lovestat/misc-code/main/emojiFace.R"
source(address, echo = T)

Reference: