42
A quick (and maybe practical) guide to Git and version control By Jay Johnson

A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

Aquick(andmaybepractical)guidetoGit andversioncontrol

ByJayJohnson

Page 2: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

Necessaryshoutoutsandreferencelinks

• fromslidesfromCSE380withDr.ChrisSimmons(UT-Austin)and• notesfromProf.Jean-LucThiffeault (UW-Madison)• https://betterexplained.com/articles/a-visual-guide-to-version-control/• http://www.math.wisc.edu/~jeanluc/gitlab_instructions.php• https://betterexplained.com/articles/a-visual-guide-to-version-control/• http://rogerdudler.github.io/git-guide/• https://git-scm.com/book/en/v2/Getting-Started-Git-Basics• https://docs.gitlab.com/ce/gitlab-basics/start-using-git.html

Page 3: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

Whatisversioncontrol?

• Ittracksyourfilesovertime!• Makeslifeeasier.

Page 4: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

Youprobablyhavealreadydonethis…

• Haveyoueverdonesomethinglike?...• Resume-may2015.docx• Resume-june2017.docx

• Orthis…• Seminar_20171116_Spratt_Draft1.tex• Seminar_20171116_Spratt_Draft2.tex

• Wewantanewversionwithoutdestroyingtheoldones!• Wemayevenuseasharedfoldersoyoudon’thavetoemailthingsbackandforth,andhopefullyeveryonerelabelsthemwhentheychangethings.

Thisworksforsmallprojectsbutlargeones?Awholedissertation?Notreally…

Page 5: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

Whatdoesversioncontroldo?• BackupandRestore. Filesaresavedastheyareedited,andyoucanjumptoanymomentintime.NeedthatfileasitwasonFeb23,2007?Noproblem.

• Synchronization. Letspeoplesharefilesandstayup-to-datewiththelatestversion.• Short-termundo.Monkeyingwithafileandmesseditup?(That’sjustlikeyou,isn’tit?).Throwawayyourchangesandgobacktothe“lastknowngood”versioninthedatabase.

• Long-termundo. Sometimeswemessupbad.Supposeyoumadeachangeayearago,andithadabug.Jumpbacktotheoldversion,andseewhatchangewasmadethatday.

• TrackChanges.Asfilesareupdated,youcanleavemessagesexplainingwhythechangehappened(storedintheVCS,notthefile).Thismakesiteasytoseehowafileisevolvingovertime,andwhy.

• TrackOwnership. AVCStagseverychangewiththenameofthepersonwhomadeit.Helpfulfor blamestorming givingcredit.

• Sandboxing,orinsuranceagainstyourself.Makingabigchange?Youcanmaketemporarychangesinanisolatedarea,testandworkoutthekinksbefore“checkingin”yourchanges.

• Branchingandmerging.Alargersandbox.Youcan branch acopyofyourcodeintoaseparateareaandmodifyitinisolation(trackingchangesseparately).Later,youcanmerge yourworkbackintothecommonarea.

Page 6: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

Usingversioncontrol

• Actuallyusingversioncontrolisthefirststep• PutEVERYTHINGunderversioncontrolConsiderputtingpartsofyourhomedirectoryunderVCUseaconsistentprojectstructureandnamingconventionCommitoftenandinlogicalchunks

• Writemeaningfulcommitmessages

• DoallfileoperationsintheVCSSetupchangenotificationsifworkingwithmultiplepeople

Page 7: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

Manyonlineserversofferfreecodestorage…

• wholeWikipediapageaboutit:https://en.wikipedia.org/wiki/Comparison_of_source_code_hosting_facilities

GitLab hasunlimitedprivatereposforanunlimitednumberofcollaboratorsplus itistheonlyonewhichiscompletelyopensource.

IhaveusedBitbucket foryearsandtheyhaveagreatGUIenvironment(SourceTree)butIammigratingtoGitLab.

Page 8: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

Somepossibletools

• Freetools!• RCS– revisioncontrolsystems• CVS– concurrentversionsystems• SVN- subversion• Mercurial• Git – whichwewilltalkabouttoday

• Commercial• MSVisualStudioTeamSystem• IBMRationalSoftware:• Clearcase AccuRev• MKSIntegrity

Page 9: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

WhousesGit?

Page 10: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

Whatisgit?

• DISTRIBUTEDversioncontrolsystem• Everyonehasthecompletehistory• Everythingisdoneoffline• Nocentralauthority• Changescanbesharedwithoutaserver

• Thismeansyoudon’thavetobeconnectedtoyourservertomakechanges,youcandothemwheneverandjust’push’themlater.

Page 11: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

Git tracksyourfolderinaseriesofsnapshotsintime

Page 12: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

Somequickjargon

• Repository(repo):thefolder/directoryinwhichyouaretrackingchanges• Master:thinkthetreetrunk– allchangesareonhere• Branch:Anothercopyofyourfilesystemwhereyoucanmakechangeswithnoworriesaboutbreakingthings!• Add:pickwhichfilesyouwanttochange.• Commit:Think‘savinganewversion’thisisasnapshotintimeofyourfiles• Push:SendthechangestoGitlab oryourserver• Pull:Whenaskforthechangesbyothers(oryouonothercomputers)• HEAD:Themostcurrentcommitintheworkingdirectory

Page 13: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

TheGit workflow:ThreeSteps

Page 14: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

TheGit workflow:ThreeSteps

ThebasicGit workflowgoessomethinglikethis:1.Youmodifyfilesinyourworkingtree.2.Youselectivelystagejustthosechangesyouwanttobepartofyournextcommit,whichadds onlythosechangestothestagingarea.3.Youdoacommit,whichtakesthefilesastheyareinthestagingareaandstoresthatsnapshotpermanentlytoyourGit directory.

Page 15: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

add&commit

Page 16: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

Whentocommit?

• Committingtoooftenmayleavetherepoinastatewherethecurrentversiondoesn’treallywork,oryoucan’treasonablyfindwhenthingsbroke• Committingtooinfrequentlymeansthatyoucan’ttrackyourchangesverywell anditmakesconflictsmuchmorelikely

Page 17: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

pushingchanges

Page 18: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

branching(moreadvanced)

Page 19: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

usefulhints

Page 20: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

Let’sstarttogether!

Page 21: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

Makeafolder

• mkdir mynewrepo• cd mynewrepo• git init

Page 22: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

Introduceyourselftogit

• $ git config --global user.name "John Doe" • $ git config --global [email protected]

Page 23: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

Optional:Setyourdefaulttexteditor

• ForNotepad++onwindows• git config --globalcore.editor "'C:/ProgramFiles/Notepad++/notepad++.exe'-multiInst -nosession”

• Forvim(oremacs)onUnix-based• git config –globalcore.editor vim

Page 24: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

Checkyoursettings….

• git config --list

Orforaspecifickey’svalue• git config <key>e.g.• git config user.name

Page 25: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

CreatinganSSHkeypair

• Whydowedothis?• Itmakeslogginginawholeloteasier.

Page 26: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

CheckifyoualreadyhaveanSSHkey

Git BashonWindows/GNU/Linux/macOS /PowerShell:• $ cat ~/.ssh/id_rsa.pub

WindowsCommandPrompt:• $ type %userprofile%\.ssh\id_rsa.pub

• Ifyouseesomethingstartingwith‘ssh-rsa’thenyouareset!Youcanjustskipaheadandcopyit.• Ifyoudon’t,thenyouneedtomakeone!

Page 27: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

GeneratinganewSSHkeypair

Git BashonWindows/GNU/Linux/macOS:• $ ssh-keygen

Whenaskedforafilepath,justusethedefaultbypressingenter.IfyoualreadyhaveanSSHkeypairbutwanttouseanewone,Googlehowtodoit…

Nextyouwillbeaskedforapassword,youshould*putoneinbutdon’tneedonefornow.Enteroneorskipitbypressingenter.Tochangethepasswordinthefutureuse…• ssh-keygen -p <keyname>

Page 28: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

CopytheSSHpairtoyourclipboard

macOS:• $ pbcopy < ~/.ssh/id_rsa.pubGNU/Linux(requiresthexclip package):• $ xclip -sel clip < ~/.ssh/id_rsa.pubWindowsCommandLine:• $ type %userprofile%\.ssh\id_rsa.pub | clip Git BashonWindows/WindowsPowerShell:• $ cat ~/.ssh/id_rsa.pub | clip

Page 29: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

AddthekeytoGitLab

• Goto‘SSHKeys’tabin’ProfileSettings’• Pasteyourkeyinthe‘Key’section• Giveareasonablename“Laptop”• Makesurethatcopiedeverythingstartingwith‘ssh-rsa’andendingwithyouremail.• Optionally,testyoursetup(replace‘example.com’withyourGitLabdomain)…

ssh –T [email protected]

Page 30: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

CreateanewprojectinGitLab

• getthefolderfromgitlab makeafile,addit,commititandsenditback!• $ git clone [email protected]:jayjohnson/mynewrepo.git• $ cd mynewrepo• $ touch README.md• $ git add README.md• $ git commit -m "add README" • $ git push -u origin master

Page 31: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

Mergesanddiffs

Ifsomeoneelse(oranothercomputer)madeachangeanditconflictswithwhatyouhavecommitted,youcancombinethemwith:$ git merge

Butthenyouwillhavetocommitthemagain$ git commit –m "Merged changes."

Page 32: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

Importantotherfunctions

$ git statuswhichgives"statusreport"ofthecurrentstateofyourlocalfiles,and$git diffor$ git diff paper.texwhichliststhedifferencesbetweenlocalfileandtheoriginalfile.Itisalsousefultoviewthechangehistory(orlog)forafileordirectory:$ git log paper.texFinally,toremoveafileordirectoryrun$ git rm paper.texwhichdeletesthefile,butwillalsodeleteforyoucollaboratorswhentheypullchangesfromtheserver.Notethatremovalsshouldbeexplainedinthecommitlogmessage(asshouldeverythingelse!).

Page 33: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

Sometips• Donotbeafraidtocommitchanges! Git rememberseverythingbeforeyourcommit,soitisvirtuallyimpossibletobreakanything.

• Commitchangesoften! Itisbettertohaveadetailedlogofsmallchangesthanahugenumberofsimultaneouschanges.Inparticular,ifyoudon'tcommitchangesoftenyouwillforgetwhatyouchanged(thoughliberaluseof'git diff'helpstofigurethatout).Thatsaid,acommittedchangesetshouldusuallybe'consistent,'inthesensethatitdoesn'tbreakthingsforeveryoneelse.Butsometimesitispreferabletohavesmallerchangesets thatdobreakthings,aslongasyoudon'tpushyourchangestotheserveruntildone.Justmakesurethelogmessagereflectsthis.

• Writehelpfullogmessages! Ittakesafewsecondsmorebutit'sworthit.Refertochangesinspecificindividualfiles.Yourcollaboratorswillthankyou,andyouwillthankyourselfwhenyourevisittheprojecttwoyearsdowntheline.Formultilinelogmessages,thefirstlineshouldbeasummaryofthechanges.

• Donotincludebinaries,executables,logs,etc.oranyotherfilesthatcanberecreated. Theideabehindversioncontrolistokeeptrackofmeaningfulchangestofiles.Binaryfilestendtochangeoften,andaredesignedtoberecreatedfromsources.Logfilesalsochangeoften,sincetheyusuallycontaindates.Unlessthebinaryorloginformationistrulyessential(i.e,thePDFfiguresinapaper),theseshouldnotbeversioned. Seenextslide forhowtoavoidthemshowingupon'gitstat'.

Page 34: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

Excludecertainfiletypeson$ git status.gitignore file:Toavoid unversioned filesshowingupon'git stat',whichisdistractingandmightleadtoyounotnoticingchanges,addtheirfilenamestothespecial .gitignore file,whichlivesatthebaseoftheprojectfolder.Forexample,whendealingwithLaTeX documents,my .gitignore fileusuallycontains*.aux*.bbl *.blg *.log.DS_Store ThefinallineignorestheclutterfilesgeneratedbyOSX.Notethatthefile .gitignore shoulditselfbeunderversioncontrol('gitadd.gitignore').

Page 35: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

replacelocalchanges

Page 36: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

Goingbacktoanearlycommit

• Greatflowchartfordecidinghowtogobacktosomethingolder…• https://raw.githubusercontent.com/emmajane/gitforteams/master/resources/workflow-undoing-changes.png• Moreinfoat:• https://stackoverflow.com/questions/4114095/how-to-revert-git-repository-to-a-previous-commit• Simpleway

• Findwhichcommityouwanttogobacktowith(rememberthesha1#whereyouaregoing• $ git log

• Gotothatcommityouwant• $ git checkout <hash>

• Gobacktowhereyouwerewith• $ git checkout -

Page 37: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

git logtips

• ifyouwanttogobackandseewhatyoucommitted(withoutgoingonlinetoGitlab)use$git log• options

• onlyshowXnumberofentries• $ -[number]

• showeachononeline• $ --pretty=oneline (can also be ‘short’, ‘full’, etc..)

• showthedifferenceofeachcommit• $ -p

• simpleclean• $ git log -oneline –n 10

• Ex:$ git log -3 --pretty=oneline• somanywaystocustomizewhatyouwanttosee• https://git-scm.com/book/id/v2/Git-Basics-Viewing-the-Commit-History

Page 38: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

Moreadvancedfeatures…

Page 39: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

newbranchandswitchtoit

Page 40: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

update&merge

Page 41: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

tagging

Page 42: A quick (and maybe practical) guide to Gitand version controlsites.utexas.edu/asa-student-chapter/files/2016/04/Grad_Skills_Seminar... · •Resume-may2015.docx •Resume-june2017.docx

Moreinfo/references

• https://betterexplained.com/articles/a-visual-guide-to-version-control/• http://rogerdudler.github.io/git-guide/• https://git-scm.com/book/en/v2/Getting-Started-Git-Basics• https://docs.gitlab.com/ce/gitlab-basics/start-using-git.html