F
for fullscreenPress
W
for widescreen
@darwinanddavis
F
for fullscreenW
for widescreen
git
and Github?git
and Github? (cont …)require(RCurl) script <- getURL("https://raw.githubusercontent.com/darwinanddavis") eval(parse(text = script))
-> commands are few for benefits a many
  Â
telnet towel.blinkenlights.nl # watch movie in ascii
R
project with self-contained filesR Studio
in-built command line connected to your GithubR
POOR
GOOD
N <- 20 # set rep number p <- rep(rnorm(100), N) # repeat a random normal dist N times
add
and commit
your filespush
your files to your GithubInitialise your new local repo
# initialise your local git git init
Check what's happening
ls # list files ls -a # list all hidden 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
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
Now we push the changes we made from our local repo to our Github cloud.
First, copy the Github repo link you want to push to. Select either https or SSH (requires key access).
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
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
R Studio
in-built command line connected to your GithubR
Open RStudio
File >
New Project >
New Directory >
New Project >
Create a git repository
R Studio
in-built command lineCreate your R
file/s
File >
New file
File options:
Using RMarkdown
Shiny web app
R Presentation
git
commands from R
terminalRStudio
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
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
git
schooled meAccidently uploaded my Google API to Github
Github emailed me with a warning
I deleted all the instances of the API, but git tracked it
Deleted the Github repo completely to remove evidence
Pushed the local files to the new repo … with all its current git history
gitignore
filesType 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
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
# 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
git
syntaxgit 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
shell
syntaxchange working dir to 'Documents'cd ~/Documents
move one level upcd ..
print current working dirpwd
list files in working dirls
make new working dirmkdir newfolder
create new filetouch text.txt
copy files from source to destination. e.g. cp /Users/mydir/README.txt ~/Documentscp source destination
copy all folders, subfolders, and files from source to destinationcp -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 filesmv ~/Desktop/MyFile.rtf ~/Desktop/MyFile-old.rtf
cp ~/Desktop/MyFile.rtf ~/Documents/MyFile-old.rtf
Exiting command editor
Write your message at the top of the editor, then run the following:
Hit ESC:
= opens editorw
+ ENTER = write (save)q
+ ENTER = quitq!
+ ENTER = quit w/o saving
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.
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
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
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
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
Accessing your SSH key:
cat ~/.ssh/id_rsa.pub
ls ~/.ssh/*.pub
Use a custom command line editor
Use your own editor for running git commands
Some examples:
Nano
Vim
Sublime
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
Install fswatch
https://github.com/emcrisostomo/fswatch (requires Homebrew package manager for Max OSX).
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>>
# <<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>>
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.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
invalid active developer path (xcrun: error: invalid active developer path)