Biostatistical Computing, PHC 6068

GitHub

Zhiguang Huo (Caleb)

Monday October 1, 2018

Outline

Why Git 1 (Version control)

Why Git 2 (Version control)

Why Git 3 (Version control)

You worked on a collabotative project with your teammates

Why Git 4 (Version control)

You worked on a collabotative project with your teammates

Benefits of version control

A typical Git (version control) work flow

Basic command on Git

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

The repository

Initialize a git repository

cd Desktop
mkdir testGit
cd testGit
git init

Make changes in the git repository

git add XXX
git commit -m "your message"

Example to make change in git repository (R part)

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

Connect the local repository with GitHub

More on the remote

Set up GitHub and connect with your local repository

git remote add origin https://github.com/Caleb-Huo/GatorPKG.git
git push
git push --set-upstream origin master ## for the first time 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/GatorPKG")
library(GatorPKG)
f(1,2)
?f

More on the remote

Two ways to connect your PC with the remote repository

Add README.md on GitHub

# testGatorPKG
This is a fancy R package

## how to install the R package
devtools::install_github("Caleb-Huo/GatorPKG")

## example
f(1,2)

Keep updated from the remote

Clone a existing remote repository to your local PC

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

Fork a repository on your GitHub account

Initialize the repository from GitHub

git clone https://github.com/Caleb-Huo/GatorPKG2.git
git add -A
git commit -m "touch"
git push

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
devtools::use_vignette("tutorial")

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

Go back to a previous snapshot

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, if you don’t want other people see your code history

.gitignore

GatorPKG.Rproj
.DS_Store
*/.DS_Store

Commonly used git commands

Reference:

Many contents are from the following resources: