Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson...

46
Version Control with Git Kaylea Nelson

Transcript of Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson...

Page 1: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

VersionControlwithGit

KayleaNelson

Page 2: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

VersionControlwithGit

•  WhatisVersionControlandGit?•  Pu8ngYourCodeintoGit•  Connec<ngYourRepositorytoBitbucket•  U<lizingYourRepository’sHistory•  Collabora<on:MergingandConflicts

Page 3: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m
Page 4: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

WhatisVersionControl?

“Thewholeideabehindanyversioncontrolsystemistostore“safe”copiesofaprojectsothatyouneverhavetoworryaboutirreparablybreakingyourcodebase.”

–Bitbucket.org•  Easyandpowerfulwaytotrackchangestoyourwork•  Usefulforbothwri<ng(ifusinge.g.LaTeX)andcode•  Backupsofyourwork•  Generalcodingsafetynet

Page 5: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

WhatisGit?Howdoesitwork?Gittrackschangestoafile(orsetoffiles)throughaseriesofsnapshotscalled“commits”or“revisions”.

Thesesnapshotsarestoredina“repository”whichcontainsahistoryofallthechangestothefiles.

Page 6: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

HowisGitusefultome?

•  “Whyisn’titworkingallofasudden?”•  Cleanerfilesystem(nomore“code,codev2,codev3_test,codev3_test1”directories)

•  Recordofyouredits(andthoughtprocess!)•  Checkforbugsininconsistentresults•  Unlimitedandpowerful“undo”•  Collabora<on!

Page 7: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

Pu8ngYourCodeintoGit

Page 8: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

ConfigureGit

•  Globalconfigura<onsforGit$ git config --global user.name "Your Name"$ git config --global user.email "[email protected]"

Page 9: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

SetupRepository

•  Ini<alizerepository

Thiscreatea.git directoryinyourdirectorythatcontainsalltheversioncontrolinforma<on.DONOTDELETE!!!

$ git init

$ ls -a. .. .git

Page 10: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

AddExis<ngFilestoRepository

•  TheGitrepositorycanbeini<alizedbeforeoraeeryoucreateanyfiles.Toversioncontrolexis<ngfiles,justaddthemtotherepository.

$ git add myplot.py

Page 11: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

CheckStatusofRepository

$ git statusOn branch master

Initial commit

Changes to be committed: (use "git rm --cached <file>..." to unstage)

new file: myplot.py

Untracked files: (use "git add <file>..." to include in what will be committed)

myfigure.png

Page 12: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

MakeIni<alCommit

•  Nowwewanttopermanentlysavethechangestorepository,anac<oncalled“commi8ng”$ git commit –m "initial commit"

Page 13: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

LeavingFilesOutofRepository

•  Youdon’twanttoaddeveryfiletoyourrepository.Thegoodruleofthumbistoexcludefilesiftheyareaproductofyourcode.Examplesoffilestoexclude:–  Imagefiles– PDFs– Compiledcode(including.oor.pycfiles)– Systemfiles(e.g.,.DS_Store)

Page 14: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

AutomateExclusions•  Toeasilyautomateexclusions,createa.gitignore file.

•  Nowthesefileswon’tshowupas“untracked”inthegit status commandandcan’taccidentallygetaddedtotherepository

$ cat .gitignaore.DS_Store*.png*.pyc

$ git add .gitignore$ git commit –m "added .gitignore"

Page 15: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

MakeChanges!

•  Makechangestothefileandthencheckontherepository$ git statusOn branch masterChanges not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)

modified: myplot.py

no changes added to commit (use "git add" and/or "git commit -a")

Page 16: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

MakeChanges!

•  Addandcommityourchanges

$ git add myplot.py$ git commit -m "increased frequency"[master 21e2dd2] increased frequency 1 file changed, 1 insertion(+), 1 deletion(-)

Page 17: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

ReviewChanges

•  Youcanchecktoseewhathasbeenmodifiedbeforeaddingfilesusinggit diff$ git diffdiff --git a/myplot.py b/myplot.pyindex 3c179cc..3eb9a45 100644--- a/myplot.py+++ b/myplot.py@@ -2,7 +2,7 @@ import matplotlib.pyplot as plt import numpy as np

t = np.arange(0.0, 2.0, 0.01)-s = np.sin(2*np.pi*t)+s = np.sin(4*np.pi*t)

plt.plot(t, s) plt.xlabel('time (s)')

Page 18: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

ReviewChanges

•  Youcanchecktoseewhathasbeenmodifiedbeforecommi8ngusinggit diff --staged$ git diff --stageddiff --git a/myplot.py b/myplot.pyindex 3c179cc..3eb9a45 100644--- a/myplot.py+++ b/myplot.py@@ -2,7 +2,7 @@ import matplotlib.pyplot as plt import numpy as np

t = np.arange(0.0, 2.0, 0.01)-s = np.sin(2*np.pi*t)+s = np.sin(4*np.pi*t)

plt.plot(t, s) plt.xlabel('time (s)')

Page 19: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

Wri<ngaGoodCommitMessage•  Thecommitmessageshouldbeahighlevelexplana<onofthechange– Don’tbetoobrief– Also,don’texactlyquotethechange

•  Example:–  Bad:“Changes”–  Bad:“Changedline178inplot_bM_vs_t.py”–  Beier:“Changecolorofpressurelinetored”

•  Mostimportantques<on:Ifyouarelookingatthismessagein6months,isitgoingtomakesenseandbeuseful?

Page 20: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

OtherUsefulCommands

•  Renameormoveafileintherepository

•  Deleteafilefromtherepository

$ git mv <old_filename> <new_filename>

$ git rm <filename>

Page 21: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

Connec<ngYourRepositorytoBitbucket

1.  Createrepositoryonyouronlineaccount2.  Followincludedinstruc<onstogetyourlocal

repositoryconnectedtoyourremoterepository

3.  Pushcommiiedchangestotheremoterepository…$ git commit -m "<message>"$ git push

Page 22: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

CreateaNewRepositoryonBitbucket

Page 23: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

CreateaNewRepositoryonBitbucket

Page 24: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

CreateaNewRepositoryonBitbucket

Page 25: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

FollowtheInstruc<ontoPush

Page 26: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

PushFutureCommits

•  Aeertheini<al“push”totheremoterepository,justremembertopushanynewcommitsandyouwillhaveeasyremotebackupsofyourwork!

…$ git commit -m "<message>"$ git push

Page 27: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

RemoteRepositoryHosts Op<ons

•  Bitbucket.org–unlimitedfreepublicandprivatereposwith.eduemailaddress

•  github.com–unlimitedfreePUBLICrepos•  git.yale.edu–freefullyfeaturedaccounts.OnlyavailableonYalenetworkorVPNandwithYalene<d

Page 28: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

U<lizingYourRepository’sHistory

Page 29: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

ReviewHistory

•  Youcanseeahistoryofallrecentcommits– DetailedLog:

– SimplifiedLog

$ git log$ git log -1

$ git log --oneline$ git log --oneline --graph --decorate

Page 30: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

CompareRevisions

•  Youcancomparetworevisionstoseewhatchangesweremadewithgit diff$ git diff HEAD 0c7aa71diff --git a/myplot.py b/myplot.pyindex 3c179cc..3eb9a45 100644--- a/myplot.py+++ b/myplot.py@@ -2,7 +2,7 @@ import matplotlib.pyplot as plt import numpy as np

t = np.arange(0.0, 2.0, 0.01)-s = np.sin(2*np.pi*t)+s = np.sin(4*np.pi*t)

plt.plot(t, s) plt.xlabel('time (s)')

Page 31: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

ReviewHistory

•  TheinterfacesonBitbucketandGithubarealsogreatforexploringthecommithistoryandtrackingchanges

Page 32: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

CheckoutPreviousCommits

Page 33: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

CheckoutPreviousCommits

•  Aeer,youhaveiden<fiedtherevisionyouneedtorevertto,“checkout”thatrevision

•  Orjustaspecificfilefromthatrevision

Warning:Ifyoucheckoutfromanoldrevision,anyuncommiiedchangestotheprojectwillbelost.

$ git checkout <revision>

$ git checkout <revision> <filename>

Page 34: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

ThrowawayAllNewChanges

•  Revertyourworkingdirectorytothelastcommit

Warning:Anyuncommiiedchangestotheprojectwillbelost.

$ git reset --hard

Page 35: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

Ge8ngYourCodeinaNewLoca<on

•  Ifyouhavearemoterepository,youcan“clone”ittoanewloca<ontocon<nueyourwork(e.g.,copyingcodetothecluster,recoveringyourcodetoanewlaptop)

$ git clone https://[email protected]/kayleanelson/my_latest_work.git

Page 36: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

Collabora<on

•  Onceyourworkisinaremoterepository,itisveryeasytobeingtocollaboratewithothers– Githasasophis<catedsystemformanagingmul<plepeopleedi<ngthesamecodebasethrough“merging”

•  UsageExamples– Mul<plecollaboratorsonacode– LaTeXpapers!

Page 37: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

SharingYourRepository

Page 38: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

BasicCollabora<veWorkflow

•  Pulldownnewcommits•  Makeyouredits•  Addyourmodifiedfilesandcommit•  Pushcommitstoremote

$ git pull…$ git add <files>$ git commit –m <message>$ git push

Page 39: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

Merging

Page 40: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

Conflicts

•  Inevitably,youandyourcollaboratorwillcommitoverlappingchangestoafile.Thiswillcreatea“mergeconflict”.

Page 41: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

ResolvingConflicts

•  PullincommitsandOops!$ git pullAuto-merging myplot.pyCONFLICT (content): Merge conflict in myplot.pyAutomatic merge failed; fix conflicts and then commit the result.

Page 42: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

ResolvingConflicts

•  Gitmarkstheconflictedlineinthefile

•  Manuallymergethecodeinatexteditorandcommitthechanges

$ cat myplot.pyimport matplotlib.pyplot as pltimport numpy as np

t = np.arange(0.0, 2.0, 0.01)<<<<<<< HEADs = np.sin(3*np.pi*t)=======s = np.sin(4*np.pi*t)>>>>>>> 7232b521f34cf3deed50f4d8aac6260616683ddf

Page 43: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

UncommiiedConflicts

•  Gitwillalsocomplainifyoupullinchangestoafileyouhavemodifiedbutnotcommiied.Youhavetwoop<ons.– UndothechangestothefilebacktolastcommiiedrevisionbycheckingitoutfromtheHEAD

– Commityourchangesandthenredothepull(andpoten<allymergethechanges,ifapplicable)

$ git checkout -- <filename>

Page 44: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

Ques<ons?

Tosummarize,add3commandstoyourdailyworkflowforunlimitedundoandonlinebackupsofyourcode!

$ git add <files>$ git commit –m <message>$ git push

Page 45: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

try.github.io

Page 46: Version Control with Git - Yale Center for Research … · Version Control with Git Kaylea Nelson ... DO NOT DELETE!!! $ git init ... $ git add  $ git commit –m

Evenmoreinforma<on:

•  Greatindepthtutorialonallthingsgit:– hips://www.atlassian.com/git/tutorials

•  SoewareCarpentry(thanksfortheimages!)– hips://swcarpentry.github.io/git-novice/01-basics/