Presentation 19.1: Git and github

Murray Logan

25 Jul 2018

Background

Required

Version control

Version control

Version control

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:

Git - overview

Files can be in one of four states:

Git - overview

Files can be in one of four states:

Git - overview

Files can be in one of four states:

Git - overview

Git - overview

Git - configuration

git config --global user.name "Your name"
git config --global user.email "your_email@whatever.com"

Git - new repository

mkdir ~/tmp/Test_repo
cd ~/tmp/Test_repo
git init
Initialized empty Git repository in /home/murray/tmp/Test_repo/.git/

RStudio

Git - adding content

Create a file (text, code etc)

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

Git - adding content

Git - .gitignore

RStudio

Git - status

git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   analysis.R

RStudio

Git - committing

git commit -m 'Initial commit'
[master (root-commit) 44e2d24] Initial commit
 1 file changed, 3 insertions(+)
 create mode 100644 analysis.R

RStudio

Git - committing

Git - additional edits

x=seq(1, 10, len=1)
y=40*2 + rnorm(10,0,5)
plot(x,y)
summary(x)
[master 91c47a6] Added summary for x
 1 file changed, 1 insertion(+)

Git - additional edits

analysis.R

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

summary.R

summary(x)
summary(y)
[master 0d7cc28] Added summaries for x and y
 2 files changed, 2 insertions(+), 1 deletion(-)
 create mode 100644 summary.R

Git - additional edits

Git - history (logs)

git log --oneline --graph --decorate
* 0d7cc28 (HEAD -> master) Added summaries for x and y
* 91c47a6 Added summary for x
* 44e2d24 Initial commit

RStudio

Git - tags

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

For example:

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

     

RStudio

Git - tags

Rolling back to previous snapshots

Git - rolling back

  1. checkout
  1. reset
  1. revert

Git - checkout

git checkout #

# is a commit or tag name

git checkout  91c4

    

RStudio

Git - checkout

Git - checkout

Restore the HEAD to the tip of master

git checkout master
Previous HEAD position was 91c47a6... Added summary for x
Switched to branch 'master'

    

Git - checkout

Git - reset

git reset --hard #

# is a commit or tag name

cd ~/tmp/Test_repo
git reset --hard  91c4

    

RStudio

Git - reset


    

Git - reset

Restore HEAD to the tag V.1

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

    

Git - reset

Git - revert

git revert HEAD --no-edit
[master 49adc9e] Revert "Added summaries for x and y"
 2 files changed, 1 insertion(+), 2 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

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 master

RStudio

Git - switch branch

Git - branch

We are on the master branch

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

For example:

git diff master Experimental
diff --git a/analysis.R b/analysis.R
index 9b5dda9..660b1bc 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 master and Experimental'
Merge made by the 'recursive' strategy.
 analysis.R | 1 +
 1 file changed, 1 insertion(+)

RStudio

Use the shell

Git - merge branches

Remote repositories and github

New remote repo

New remote repo

Set remote

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

RStudio

Use the shell ….. Um Luke!

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 master

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 master

Playtime

Resources

Useful resources