@darwinanddavis

Press F for fullscreen
Press W for widescreen

Location

What the hell is git and Github?

Version control system for data

Logs commits of file changes retrievable at any time

What the hell is git and Github? (cont …)

Applications

Version control

Collaborate

Centralised storage of every possible file type, e.g. Supp Material

Dynamic loading of stored links and programs

require(RCurl)
script <- getURL("https://raw.githubusercontent.com/darwinanddavis")
eval(parse(text = script))

Fork and clone a plethora of public data, code, material

But why?

Reproducible

Unlimited

Transparent

Shareable

Why use the command line?

- Complete control and ease of workflow

- Transparent history

- Automate your commands, e.g. schedule an automatic daily backup

- A steeper learning curve, but worth the investment

-> commands are few for benefits a many 

Why use the command line (cont …)?

     

telnet towel.blinkenlights.nl # watch movie in ascii  

Workflow example

Open an R project with self-contained files

Use the R Studio in-built command line connected to your Github

Backup straight from R

Cake!

Best practice for git prep

Avoid spaces and CamelCase

  • e.g. 'my data.csv', 'My Data.csv' POOR
  • e.g. 'mydata.csv', 'my_data.csv' GOOD

Annotation

N <- 20  # set rep number  
p <- rep(rnorm(100), N)  # repeat a random normal dist N times  

Tab is your friend

Let's git it

Initialising and using your repo

1. Create a repo

2. Create and stage your files

- add and commit your files

3. Push to a remote github repo

- push your files to your Github

1. Create a repo

Initialise your new local repo

# initialise your local git
git init  

Check what's happening

ls # list files 
ls -a # list all hidden files

2. Create and stage your files

Add the files in your folder to the local git repo

git add . # the '.' adds all files in your current directory  

Stage the files for the commit

git commit -m 'init commit' # -m adds a message  

We've now added and staged files to a local repo. Version control!

Let's check the changes

git log # recent git activity
git add test.txt # adds individual files  
git status # check what git is doing 
git reset # reset latest commit 

3. Push to a remote Github repo

Now we push the changes we made from our local repo to our Github cloud.

  1. Create a new Github repo. Name is using best practice, e.g. no spaces
  2. Don't create a README. Yet.
  • Uncheck the box 'Initialise this Github with a README'.

3. Push to a remote Github repo (cont …)

First, copy the Github repo link you want to push to. Select either https or SSH (requires key access).

3. Push to a remote github repo (cont …)

Then push your staged (commit) files from your local repo to the remote repo

# set the new remote repo
git remote add origin "your github repo"  # if remote branch doesn't exist
# see what remote repositories you have  
git remote -v  
# push changes to remote origin (github) from master branch (local)       
git push origin master 

That's it!

Your data is now stored and version controlled
in local and remote repos

Cloning an existing repo

Clone a remote repo to your local computer

Clone a remote repo to your local computer (cont..)

This creates a git repository on your local machine complete with version control.

Every version of every file for the history of the project is grabbed by default when you run git clone.

git clone "github url" "new repo name (optional)"
# e.g. git clone https://github.com/darwinanddavis/UsefulCode mynewrepo 

Why clone?

You can dump the contents of any public repo, including its complete version history, onto your own computer, then upload it onto the cloud.

Workflow example

Open an '.Rproj' with self-contained files

Use the R Studio in-built command line connected to your Github

Backup straight from R

Cake!

Open an '.Rproj' with self-contained files

Open RStudio

File >
New Project >
New Directory >
New Project >
Create a git repository

Use the R Studio in-built command line

Run your git commands from R terminal

One more option

  1. Go to GIT menu in RStudio

Examples of my workflow

  • Useful Code repo and README.md file

Using the README file

README Option 1

You unchecked the 'Initialise with a README' option on Github

Your active local repository

git init # initialise local repo  
git remote add origin "your Github repo"  # add Github repo (remote = cloud)  

Create a README using a text editor, e.g. Sublime (md = Markdown)

git add README.md # stage the readme file 
git commit -m 'readme' # commit the git  
git push origin master # push the changes to your Github  

README Option 2

You checked the 'Initialise with a README' option on Github

Your active local repository

git init # initialise local repo  
git remote add origin "your Github repo"  # add Github repo (remote = cloud)  

Pull the new change (a commit) that you made to the repo by initialising the README

git pull origin master # pull the readme file change from Github   
git push --set-upstream origin master # push the changes to your Github  

Check git status

git status # check git status

The time git schooled me

  1. Accidently uploaded my Google API to Github

  2. Github emailed me with a warning

  3. I deleted all the instances of the API, but git tracked it

  4. Deleted the Github repo completely to remove evidence

  5. Pushed the local files to the new repo … with all its current git history

gitignore files

Type the following in Terminal/R

ls -a # lists current dir contents   
touch .gitignore  # create a gitignore if not already
open .gitignore # open the gitignore file    
git status --ignored # list what is being ignored 

The short version

local git (version control on your comp)

git init # initialise your local git  
git add . # adds all files to git. replace '.' with filename for individ files
git commit -m 'redo intro' # '-m' = message 

remote git (version control on your github)

# after the above steps ^ 
# see what remote repo you have. if a github exists, you can push
git remote -v  
# set the new remote repo (if necessary)  
git remote add origin "your github repo"  # if remote branch doesn't exist
git remote set-url origin "your github repo"  # if already exists
# push changes from local repo to remote repo 
git push origin master 

If in doubt, ask the internet

Troubleshooting

Useful git syntax

git config --global user.name "Matt Malishev"
git config --global user.email "mmlshv@gmail.com" 

git diff

git diff # print differences (changes) to files  
git diff --stage # show changes in staged gits 

Extra flags for git commands

git log --online # condense log into one line summary  

Useful shell syntax

change working dir to 'Documents'
cd ~/Documents

move one level up
cd ..

print current working dir
pwd

list files in working dir
ls

make new working dir
mkdir newfolder

create new file
touch text.txt

More useful syntax

copy files from source to destination. e.g. cp /Users/mydir/README.txt ~/Documents
cp source destination

copy all folders, subfolders, and files from source to destination
cp -R source destination

move files or folders from source to destination (no need for -R)
mv source destination

move multiple files with the * wildcard, which copies all .rtf files. The tilde (~) symbol is a shortcut for your Home folder, which contains '/Desktop'.
cp ~/Desktop/*.rtf ~/Documents

rename files
mv ~/Desktop/MyFile.rtf ~/Desktop/MyFile-old.rtf
cp ~/Desktop/MyFile.rtf ~/Documents/MyFile-old.rtf

Exit the command editor

Exiting command editor
Write your message at the top of the editor, then run the following:

Hit ESC
: = opens editor
w + ENTER = write (save)
q + ENTER = quit
q! + ENTER = quit w/o saving

Exit the command editor (cont..)

Example

readme # enter message

Type :wq, then press ENTER.

Nano:
Press Control+O (the letter, not 0 the number), then Enter to save the message. Then, press Control+X to exit.

Vim:
Press Escape and then type :wq and press Enter.

Common errors

fatal: remote origin already exists
The remote origin already exists, so you can't add it again

git remote rm origin # if origin already exists, remove it
git remote add origin "your github repo" # then re-add 
git push origin master # then push again  

! [rejected] master -> master (non-fast-forward) Someone else has made changes since your latest ones and git refuses to lose the commit, so won't push your new changes

git pull origin master # fetches any updates to online repo and merges them    

Common errors (cont …)

fatal: refusing to merge unrelated histories Usually associated with a README file on the Github repo

git pull origin master --allow-unrelated-histories # unnecessary parallel history 
# merged to your project. usually associated with a README.md file

If VIM opens, type 'SHIFT + :', then press ENTER

fatal: The current branch master has no upstream branch

git push --set-upstream origin master

Weird technical errors

invalid active developer path (Mac OSX and XCode issue)
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

xcode-select --install # install missing xcode developer tools
xcode-select --reset # if above install doesn't work, reset xcode 

Staging and pushing files

Re-do a commit

git reset --soft HEAD~1

Alternative push option

# option 1
git remote set-url origin "link to existing github repo" # talk to github 
git push -u origin master
# option 2
git remote add github "your github repo"  # if remote branch doesn't exist
git push -u github master

Staging and pushing files (cont …)

After pushing to your remote repo and this error appears:
! [rejected] master -> master (fetch first)

git fetch origin master # match the local repo commit status to the push destination     
git merge master # merge the recent commits    
git push -u origin master # push to remote repo  

# ------- for non-fast-forward error  ---------
# grab changes made on remote repo and align with local master branch 
git fetch origin master:tmp  
git rebase tmp
git push origin HEAD:master # push the changes from local HEAD to remote    
git branch -D tmp
git push -u origin master # finalise the changes  

Staging and pushing files (cont …)

For fatal: refusing to merge unrelated histories error

git checkout master
git merge origin/master --allow-unrelated-histories
# or run this before your 'git pull origin master' command  
git pull --allow-unrelated-histories origin master 

Delete files from remote repo (option 1)

git rm --cached file1.txt
git commit -m "remove file1.txt"
git push

Staging and pushing files (cont …)

Delete files from a Github repo (option 2)

# ensure you are in the default branch:
git checkout master
# the rm -r command will recursively remove your folder:
git rm -r folder-name
#Commit the change:
git commit -m "Remove duplicated directory"
# push the change to your remote repo
git push origin master

Accessibility

If Github questions your user credentials.

git config --global user.email "<your email>" 
git config --global user.name "<your github user name>" 

Cache your user credentials to avoid being asked everytime

# once in git directory   
git config credential.helper store  

Accessibility (cont …)

When using SSH for your github remote repo, e.g. git@github.com:username/reponame.git

Generating a new SSH key

Accessing your SSH key:

  • In Mac, in Terminal, type
cat ~/.ssh/id_rsa.pub  
  • In Windows, in cmd, type
ls ~/.ssh/*.pub   

Accessibility (cont …)

Accessing commits

How to undo anything with Git

How to access recent commits to your local repo

git log # check recent activity and select commit e.g. 0df4g3 ...  
git checkout "enter your commit tag" # e.g. 50577c90  
git checkout master  # return to current branch 
git revert HEAD~3 # revert back 3 commits 

To revert everything from the HEAD (current stage) back to the commit hash

git revert --no-commit 0766c053..HEAD # replace 0766c053 with your commit tag
git commit

Creating automatic commits/push commands

Creating automatic commits/push commands (cont …)

  1. In Terminal, navigate to your folder with the git
# <<file>> = file you want to monitor
# <<path/to/auto_commit_push.sh>> = path to the script created above
fswatch -0 <<file>> | xargs -0 -n 1 bash <<path/to/auto_commit_push.sh>>
  1. Keep the fswatch command still active in a separate shell, do whatever you want and when monitored file is updated, it will automatically be committed and pushed.

References

References (cont …)

References (cont …)