Transcription

A brief introduction to git & GitHubKarl Broman & Samuel G. YounkinBiostatistics & Medical InformaticsUniversity of Wisconsin–Madisongithub.com/syounkin/GitPrimer1

d 15312

Methods for tracking versions Don't keep track Save numbered zip files Formal version control3

Suppose it stops working Don't keep track– good luck! Save numbered zip files– Unzip versions and diff Formal version control– Easy to study changes back in time– Easy to jump back and test3

Why use formal version control? History of changes Able to go back No worries about breaking things that work Merging changes from multiple people4

Example repository5

Example repository5

Example history6

Example commit7

What is git? Formal version control system Developed by Linus Torvalds (developer of Linux)– used to manage the source code for Linux Tracks any content (but mostly plain text files)–––––source codedata analysis projectsmanuscriptswebsitespresentations8

Why use git? It's fast You don't need access to a server Amazingly good at merging simultaneous changes Everyone's using it9

What is GitHub? A home for git repositories Interface for exploring git repositories Real open source– immediate, easy access to the code Like facebook for programmers (Bitbucket.org is an alternative)– free private repositories10

Why use GitHub? It takes care of the server aspects of git Graphical user interface for git– Exploring code and its history– Tracking issues Facilitates:– Learning from others– Seeing what people are up to– Contributing to others' code Lowers the barrier to collaboration– "There's a typo in your documentation." vs."Here's a correction for your documentation."11

Basic use Change some files See what you've changedgit statusgit diffgit log Indicate what changes to savegit add Commit to those changesgit commit12

Basic use Change some files See what you've changedgit statusgit diffgit log Indicate what changes to savegit add Commit to those changesgit commit Push the changes to GitHubgit push12

Basic use Change some files See what you've changedgit statusgit diffgit log Indicate what changes to savegit add Commit to those changesgit commit Push the changes to GitHubgit push Pull changes from your collaboratorgit pull12

Initialize repository Create a working directory– For example, /GitPrimer Initialize it to be a git repository– git init– Creates subdirectory /GitPrimer/.git mkdir / GitPrimer cd / GitPrimer git initInitialized empty Git repository in / GitPrimer /.git/13

Produce content Create a README fileWelcome to the GitPrimer repository.Date: Tue Oct1 14:12:47 CDT 2013This repository contains source code for a brief git & GitHub tutorialgiven by Younkin & Broman at the University of Wisconsin -Madison ,Dept. of Biostatistics & Medical Informatics.Email Samuel Younkin [email protected] with questions orcomments.14

Produce content Or create a README.md fileWelcome to the GitPrimer repository.Date: Tue Oct1 14:12:47 CDT 2013This repository contains source code for a brief git & GitHub tutorialgiven by [Younkin ]( http :// www.stat.wisc.edu/ syounkin /) &[Broman ]( http :// www.biostat.wisc.edu/ kbroman) at the University ofWisconsin -Madison , Dept. of Biostatistics & Medical Informatics.Email Samuel Younkin [email protected] with questions orcomments , or submit an[Issue at the GitHub ]( https :// github.com/syounkin/GitPrimer/issues ).15

Incorporate into repository Stage the changes using git add git add README16

Incorporate into repository Now commit using git commit git commit -m "Initial commit of README file"[master (root -commit) 32 c9d01] Initial commit of README file1 file changed , 14 insertions ( )create mode 100644 README The -m argument allows one to enter a messageWithout -m, git will spawn a text editorUse a meaningful messageMessage can have multiple lines, but make 1st linean overview17

A few points on commits Use frequent, small commits Don't get out of sync with your collaborators Commit the sources, not the derived files(R code not images) Use a .gitignore file to indicate files to be ignored* manuscript.pdfFigs /*. pdf.RData.RHistory*. Rout*.aux*.log*.out18

Removing/moving filesFor files that are being tracked by git:Use git rm instead of just rmUse git mv instead of just mv gitgitgitgitrm myfilemv myfile newnamemv myfile SubDir/commit19

Using git on an existing project git init Set up .gitignore file git status (did you miss any?) git add . (or name files individually) git status (did you miss any?) git commit20

Basic use Change some files See what you've changedgit statusgit diffgit log Indicate what changes to savegit add Commit to those changesgit commit Push the changes to GitHubgit push Pull changes from your collaboratorgit pull21

Getting started with GitHub Get an account Set up ssh keys– Look for files /.ssh/id rsa and /.ssh/id rsa.pub– ssh-keygen -t rsa -C "your [email protected]"– Copy contents of /.ssh/id rsa.pub Add SSH key at GitHub–––– Account settingsSSH KeysAdd SSH keyPaste contents of /.ssh/id rsa.pubSimilar thing at BitBucket22

Set up GitHub repository Click the "Create a new repo" button Give it a name and description Click the "Create repository" button Back at the command line:git remote add origin [email protected]:username/repogit push -u origin master23

Set up GitHub repository23

Set up GitHub repository23

Configuration filePart of a .git/config file:[remote "origin"]url [email protected]:kbroman/qtl.gitfetch refs/heads /*: refs/remotes/origin /*[branch "master"]remote originmerge refs/heads/master[remote "brian"]url git :// github.com/byandell/qtl.gitfetch refs/heads /*: refs/remotes/brian /*24

Issues and pull requests Problem with or suggestion for someone's code?– Point it out as an Issue Even better: Provide a mit a Pull Request25

Suggest a change to a repo Go to the repository:http://github.com/someone/repo Fork the repositoryClick the "Fork" button Clone your version of itgit clone [email protected]:username/repo Change things locally, git add, git commit Push your changes to your GitHub repositorygit push Go to your GitHub repository Click "Pull Requests" and "New pull request"26

Pulling a friend's changes Add a connectiongit remote add friend git://github.com/friend/repo Pull the changesgit pull friend master Push them back to your GitHub repogit push27

Merge conflictsSometimes after git pull friend masterAuto -merging README.mdCONFLICT (content ): Merge conflict in README.mdAutomatic merge failed; fix conflicts and then commit the result.Inside the file you'll see: HEADA line in my file. A line in my friend 's file 031389 f2cd2acde08e32f0beb084b2f7c3257fffEdit, add, commit, push, submit pull request.28

git/GitHub with RStudio29

git/GitHub with RStudio29

git/GitHub with RStudio29

git/GitHub with RStudio29

git/GitHub with RStudio29

git/GitHub with RStudio29

git/GitHub with RStudio29

git/GitHub with RStudio29

git/GitHub with RStudio29

git/GitHub with RStudio29

git/GitHub with RStudiogit remote add origin [email protected]:kbroman/repogit push -u origin master29

git/GitHub with RStudio29

git/GitHub with RStudio29

Delete GitHub repo30

Open source means everyone can see my stupidmistakes.Version control means everyone can see every stupidmistake I've ever made.bit.ly/stupidcode31

Resources Look at others' repositories:– Hadley Wickham (ggplot2): https://github.com/hadley– Yihui Xie (knitr): https://github.com/yihui Karl's tutorial: Karthik Ram's slides: Pro Git book:http://kbroman.github.io/github tutorialhttp://karthikram.github.io/git introhttp://git-scm.com/book32