Introduction to Biostatistical Computing PHC 6937

GitHub

Zhiguang Huo (Caleb)

Monday Oct 10th, 2022

Outline

Git

Benefits of version control

Basic command on Git

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

Git basics

Git basics (The three states)

Git workflow

Initialize a git repository

cd Desktop
mkdir testGit
cd testGit
git init

The repository

Make changes in the git repository

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)
##' 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

git checkout

Go to previous snapshot

git checkout SHA1number

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/Caleb-Huo/GatorPKG3.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 GatorPKG3 package

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

git add -A
git commit -m "this is a R pacakge"
git log
git push

Git Push now requires a token

  1. Go to setting
  2. Developer settings
  3. Personal access tokens
  4. Generate new tokens
    • Make any note
    • select at least repo
    • Generate token
  5. Copy the token (password) and paste it in the terminal

reference: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token

Exercise: put the R package on GitHub

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

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

devtools::install_github("Caleb-Huo/GatorPKG3")
library(GatorPKG3)
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("Caleb-Huo/GatorPKG3")

## 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 a 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

Other files in the repository

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

Git whole picture

Create a another branch

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

Git branches

Exercise for creating a another branch

git checkout -b vignette
usethis::use_vignette("GatorPKG3")
git commit -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

Delete commits

Compare with another commit object

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

Git merge (when there are no conflicts)

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

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

Git merge (when there are conflicts)

git checkout main
git merge --no-ff vignette ## fail
git diff ## check the difference
## open the conflict file, resolve conflicts
git commit -a -m "conflict resolved"

Display html on github

Summarize all important things about Git/GitHub

Reference:

Things you should have done, by the end of this week