/githubpres

Workshop on using git and Github

Primary LanguageHTML

Using Github for research and life

Matthew Malishev1*

1 Department of Biology, Emory University, 1510 Clifton Road NE, Atlanta, GA, USA, 30322
*Corresponding author: matthew.malishev [at] gmail.com

Workshop on using git and Github (this thing you're on now) for version control, project collaboration, and data sharing
Department of Biology, Emory University
March 19, 2019


File extensions:
.Rmd
.html
.pdf
.tex
.jpeg
.png
.gif


1️⃣ Installation instructions

Download the instructions for installing git for both Mac OSX and Windows.

2️⃣ Seminar slides

Right click and select 'Save as', then open the HTML on your preferred browser:

Using Git with GUI (Part I)

Using Git with Terminal (Part II)

The short version (with Terminal)

Local git (version control on your computer)

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

🐷 Troubleshooting

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

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

For exiting editor on merge or pull

press "i"
Write your merge message
Press "esc"
Write ":wq"
Then 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    

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

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  

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

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  

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   

Accessing commits {.build}

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"  
git checkout master  # return to current branch 

Creating automatic commits/push commands {.build}

  1. Install fswatch https://github.com/emcrisostomo/fswatch (requires Homebrew package manager for Max OSX).

  2. Create a script for the commit and push (auto_commit_push.sh)

#!/bin/bash
# <<branch>> = branch you are pushing to
git commit -m "auto commit" $1 
git push origin <<branch>> 

Creating automatic commits/push commands (cont ...) {.build}

  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

Generating a new SSH key

How to undo anything with Git

How to access recent commits to your local repo

Origin master - rejected (fetch first), no file in GitHub repository

Intro to git: Branches, pull requests, and other useful stuff

Automatically push an updated file whenever it is changed

Using git and Github with R: Happy Git and Github for the useR

Creating and Hosting a Personal Site on GitHub

Switching between terminal and GUI

Using Github pages

Using RMarkdown

Shiny web app

Authoring R presentations

invalid active developer path (xcrun: error: invalid active developer path)

Maintainer

Matt Malishev
🔍 Website
🐦 @darwinanddavis
📧 matthew.malishev [at] gmail.com