Git and Github

Murray Logan

15 September 2024

Background

Required

  • RStudio or text editor

Version control

Version control

  • large space requirements
  • difficult to collaborate

Git

Git - overview

  • Git - is a distributed version control system
  • Git - stores snapshots of the filesystem
  • commits

  • The system consists of three trees:

Git - overview

Files can be in one of four states:

  • untracked

Git - overview

Files can be in one of four states:

  • untracked
  • staged

Git - overview

Files can be in one of four states:

  • untracked
  • staged
  • committed

Git - overview

Files can be in one of four states:

  • untracked
  • staged
  • committed
  • modified

Git - overview

Git - overview

Git in depth

Git - configuration

This is a once off action (per machine)

  • define who you are
git config --global user.name "Your name"
git config --global user.email "your_email@whatever.com"
  • Specify that the initial branch should be called main rather than master
git config --global init.defaultBranch main
  • Check these settings
git config --global --list
credential.helper=cache
user.name=pcinereus
user.email=i.obesulus@gmail.com
init.defaultbranch=main

Git - configuration

Or within R (via the usethis package)

usethis::use_git_config(user.name='Your name',
               user.email='your_email@whatever.com',
               scope='user')
usethis::git_sitrep()

OR via the gert package

gert::git_config_global()

Git - new repository

mkdir ~/tmp/Test_repo
cd ~/tmp/Test_repo
git init

RStudio

R

library(usethis)
create_project(path='~/path/project_name', rstudio=TRUE)
use_git()

Git - adding content


Create a file (text, code etc)

  • those using R, call it analysis.R
x=seq(1, 10, len = 1)
y=40 * 2 + rnorm(10, 0, 5)
plot(x, y)



Otherwise, create any kind of file (in the folder we just created)

Git - adding content

Stage the changes (add)

git add <file(s)>

For example:

git add analysis.R

RStudio

gert::git_add('analysis.R')

Git - adding content

Git - .gitignore

RStudio

Examples

  • .RData all files ending in .RData
  • .pdf all files ending in .pdf
  • data/ the entire folder called data

Git - committing

git commit -m 'Initial commit'
[main (root-commit) a929516] Initial commit
 1 file changed, 3 insertions(+)
 create mode 100644 analysis.R
cd ~/tmp/Test_repo
git status
On branch main
nothing to commit, working tree clean

RStudio

gert::git_commit('Initial commit')

Git - committing

Git - additional edits

  • Make some changes to the file, stage (add) and commit
x=seq(1, 10, len=1)
y=40*2 + rnorm(10,0,5)
plot(x,y)
summary(x)
[main cd283f5] Added summary for x
 1 file changed, 1 insertion(+)

Git - additional edits

  • Lets remove the summary(x) and add it to another file, stage (add) and commit

analysis.R

x=seq(1, 10, len=1)
y=40*2 + rnorm(10,0,5)
plot(x,y)

summary.R

summary(x)
summary(y)
[main fb73e5d] Added summaries for x and y
 2 files changed, 5 insertions(+), 4 deletions(-)
 create mode 100644 summary.R

Git - additional edits

Git - history (logs)

git log --oneline --graph --decorate
* fb73e5d (HEAD -> main) Added summaries for x and y
* cd283f5 Added summary for x
* a929516 Initial commit

RStudio

Git - history (logs)

gert::git_log(max=10)
                                    commit                           author
1 fb73e5de8c74f6387e58faff58168afe4d648465 pcinereus <i.obesulus@gmail.com>
2 cd283f515cfb1d038b9ebb8f07be391cc102a10d pcinereus <i.obesulus@gmail.com>
3 a9295166a648de8d54ad4508904ad81ba198644a pcinereus <i.obesulus@gmail.com>
                 time files merge                       message
1 2024-09-15 02:50:24     2 FALSE Added summaries for x and y\n
2 2024-09-15 02:50:24     1 FALSE         Added summary for x\n
3 2024-09-15 02:50:23     1 FALSE              Initial commit\n

A listing (data.frame) of tracked files

gert::git_ls()
        path filesize            modified             created
1 analysis.R       53 2024-09-15 02:50:24 2024-09-15 02:50:24
2  summary.R       22 2024-09-15 02:50:24 2024-09-15 02:50:24

Git - tags

git tag -a <tag> -m <message>

For example:

git tag -a 'V.1' -m 'Version 1'

R/RStudio

gert::git_tag_create(name='V1', message='Version 1')


Git - tags

Rolling back to previous snapshots

Git - rolling back

  1. checkout

  2. reset

  3. revert

Git - checkout

git checkout #

# is a commit or tag name

git checkout  cd28

R/RStudio

gert::git_branch_create(branch = 'temp', ref = 'cd28', checkout = TRUE)

Git - checkout

Git - checkout

Restore the HEAD to the tip of main

git checkout main
Previous HEAD position was cd283f5 Added summary for x
Switched to branch 'main'

R/Rstudio

gert::git_branch_checkout(branch = 'main')

Git - checkout

Git - reset

git reset --hard #

# is a commit or tag name

cd ~/tmp/Test_repo
git reset --hard cd28

R/RStudio

gert::git_reset_hard(ref='cd28')

Git - reset

Git - reset

Restore HEAD to the tag V.1

git reset --hard V.1
HEAD is now at fb73e5d Added summaries for x and y

R

gert::git_reset_hard(ref='V.1')

Git - reset

Git - revert

git revert HEAD --no-edit
[main 5ff93ae] Revert "Added summaries for x and y"
 Date: Sun Sep 15 02:50:32 2024 +0000
 2 files changed, 4 insertions(+), 5 deletions(-)
 delete mode 100644 summary.R

RStudio

Git - revert

Git - revert multiples

command line

git revert --no-commit HEAD
git revert --no-commit HEAD~1
git commit -m 'Rolled back'

RStudio

Branching

Git - new branch

git checkout -b <Name>

For example

git checkout -b Experimental

RStudio

Git - new branch

Git - branch

We are on the new branch

  • add or edit some content
    • stage and commit
x=seq(1, 10, len=1)
y=40*2 + rnorm(10,0,5)
plot(x,y)
summary(x)
mean(x)

Otherwise, create any kind of file (in the folder we just created)

Git - branch

Git - switch branch

git checkout <Name>

For example:

git checkout main

RStudio

Git - switch branch

Git - branch

We are on the main branch

  • add another file (test.R)
    • stage and commit
mean(c(1,2,3))

Otherwise, create any kind of file

Git - branch

Git - branch log

git log --online --graph --decorate --all

RStudio

Git - diff

git diff main <branch>

For example:

git diff main Experimental
diff --git a/analysis.R b/analysis.R
index 2318425..668a9b6 100644
--- a/analysis.R
+++ b/analysis.R
@@ -2,3 +2,4 @@ x=seq(1, 10, len = 1)
 y=40 * 2 + rnorm(10, 0, 5)
 plot(x, y)
 summary(x)
+mean(x)
diff --git a/test.R b/test.R
deleted file mode 100644
index 0242716..0000000
--- a/test.R
+++ /dev/null
@@ -1 +0,0 @@
-mean(c(1,2,3))

Git - merge branches

git merge <Name>

For example:

cd ~/tmp/Test_repo
git merge Experimental -m 'Merge main and Experimental'
Merge made by the 'ort' strategy.
 analysis.R | 1 +
 1 file changed, 1 insertion(+)

RStudio

gert::git_branch_checkout('main') # ensure on main branch
gert::git_merge(ref='Experimental')

Git - merge branches

Remote repositories and github

Github

  • Step 1. get a github account and verify email address

  • Step 2. create one (or more) Personal Access Tokens (PAT)

    • more nuanced than a single password
  • Step 3. create a remote repository on github

  • Step 4. push/pull between remote and local

Personal Access Token

R

  • Generate token in github
usethis::create_github_token()
  • copy token
  • store token in manager (Keychain etc)
gitcreds::gitcreds_set()
  • confirm
gitcreds::gitcreds_get()
#gh::gh_whoami()

New remote repo

From exising git and Rstudio project

usethis::use_github()

New remote repo

If not using use_github()

Set remote

If not using use_github()

cd ~/tmp/Test_repo
git remote add origin https://github.com/pcinereus/Test.git
git push -u origin main

R

gert::git_remote_add(url='https://github.com/pcinereus/Test.git')
gert::git_remote_list()

Set remote

Github - collaborate

Git - another commit

Lets make a small change to one of the files..

mean(c(1,2,3))
sd(c(1,2,3))



Otherwise, create any kind of file

Git - another commit

Git - push to repo

git push -u origin main

Clone a repo

git clone <git name> <local name>

RStudio

Git - pull

Before making any changes that you intend to push, it is advisable that you pull to get the latest from the remote

git pull -v origin main

Playtime

  • Clone the Test repo of the person next to you

  • Make a change, commit, push

  • Pull the changes of your neighbour

  • Try making a branch

Resources

Useful resources