Biostatistical Computing, PHC 6068

GitHub

Zhiguang Huo (Caleb)

Monday September 30, 2019

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)

devtools::create("GatorPKG") 
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 rm --cached <file>..

git commit

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

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)

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

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 updatd with the 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 revert

Go back to the previous snapshot by SHA1 number

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

Delete commits

Don’t suggest to do so, but just in case you don’t want other people see your code history

If you want to try, suggest to try this in an experimental branch

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 master
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 master
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:

A typical Git work flow (revisit)