Essential Git for developers

A presentation at Northeast PHP in August 2015 in Boston, MA, USA by Adam Culp

Slide 1

Slide 1

Essential Git For Developers By: Adam Culp Twitter: @adamculp https://joind.in/14744

Slide 2

Slide 2

Essential Git For Developers ● 2 About me – PHP 5.3 Certified – Work at Zend Technologies – Organizer SoFloPHP (South Florida) – Organized SunshinePHP (Miami) – Long distance runner – Judo Black Belt Instructor

Slide 3

Slide 3

Essential Git For Developers ● Fan of iteration – 3 Everything requires iteration to do well: (practice makes perfect) ● Long distance running ● Judo ● Development ● Avoid project managers ● Version Control!

Slide 4

Slide 4

Essential Git For Developers ● Why use Git? – No centralization ● – Each clone = full repository ● 4 No central server (unless desired) Git tracks state, history, and integrity – Branching and Merging work – Fast ● Local vs Remote ● Only one .git directory – Files to be committed are “staged” first – Free and Open Source – Flexible workflow

Slide 5

Slide 5

Essential Git For Developers ● How Others Looks At Data. – As files and the changes made to each file. Version 1 Version 2 File A Diff 1 Version 3 Diff 1 5 Diff 1 Version 5 Diff 2 File B File C Version 4 Diff 2 Diff 2 Diff 3

Slide 6

Slide 6

Essential Git For Developers ● How Git Looks At Data. – As whole files, not files + diffs. Version 1 Version 2 Version 3 Version 4 Version 5 File A File A1 File A1 File A2 File A2 File B File B File B File B1 File B2 File C File C1 File C2 File C2 File C3 Green means whole file, yellow means pointer to previous whole file. 6

Slide 7

Slide 7

Essential Git For Developers ● Subversion-Style Workflow Shared Repository Developer 7 Developer Developer

Slide 8

Slide 8

Essential Git For Developers ● Integration Manager Workflow Blessed Repository Integration Manager 8 Developer Public Developer Public Developer Private Developer Private

Slide 9

Slide 9

Essential Git For Developers ● Dictator and Lieutenants Workflow Blessed Repository Dictator Lieutenant Lieutenant Developer 9 Developer Developer Developer

Slide 10

Slide 10

Essential Git For Developers ● Single Developer – One repository, everything in one basket. ● Remember to backup Developer Local Repository 10

Slide 11

Slide 11

Essential Git For Developers ● 11 Each ‘git clone’ == full repository

Slide 12

Slide 12

Essential Git For Developers ● What is actually going on? – A bunch of repositories! Repository Repository 12 Repository Repository

Slide 13

Slide 13

Essential Git For Developers ● But it could be: – Repositories can connect in all directions. Repository Repository Repository Repository 13

Slide 14

Slide 14

Essential Git For Developers ● 14 Most common commands – git config – git init – git clone – git status – git add – git commit – git log or show – git branch – git checkout – git merge – git pull or push

Slide 15

Slide 15

Essential Git For Developers ● Help! – 15 Adding ‘-h’ to any command will return help on usage.

Slide 16

Slide 16

Essential Git For Developers ● 16 git config – Easily set your information to accompany commits. – Generally a one time thing.

Slide 17

Slide 17

Essential Git For Developers ● 17 git init – Instruct Git to track project by simply ‘git init’. – No more excuses! Version all the things!

Slide 18

Slide 18

Essential Git For Developers ● git clone {repo} {destination} – Creates new repository based on another. – Cloned repository becomes “Origin”. ● 18 Internal pointer for where it came from.

Slide 19

Slide 19

Essential Git For Developers ● 19 Example of ‘git clone’ – Below we clone a repo from github. – We address the .git directory. – Specify where to put it.

Slide 20

Slide 20

Essential Git For Developers ● git status – 20 Provides status of resources in the tracked project.

Slide 21

Slide 21

Essential Git For Developers ● git status – 21 Below ‘git status’ informs us of untracked files after created.

Slide 22

Slide 22

Essential Git For Developers ● git add – 22 Stages files to be committed.

Slide 23

Slide 23

Essential Git For Developers ● git commit – A ‘git commit’ includes all “staged” files. – Use ‘-m’ to store a message with the commit. ● 23 Or git prompts user to add a message. (using default editor)

Slide 24

Slide 24

Essential Git For Developers ● More on commits – A commit should be: ● Done OFTEN! ● Commit messages Always included – Short – Informative Single commit per bug or ticket. – ● 24

Slide 25

Slide 25

Essential Git For Developers ● 25 git log – Shows history of prior commits. – We’ve only done one, and here it is:

Slide 26

Slide 26

Essential Git For Developers ● 26 git show {commit hash} – Hash optional, will show previous by default. – Shows commit + diff view of files.

Slide 27

Slide 27

Essential Git For Developers ● What would a commit do? – 27 We did a ‘git add’ for file #2, and modified file 1.

Slide 28

Slide 28

Essential Git For Developers ● And now? – 28 We did a ‘git add’ for modified file 1.

Slide 29

Slide 29

Essential Git For Developers ● And finally? – 29 We did a ‘git add’ for new file 3.

Slide 30

Slide 30

Essential Git For Developers ● 30 After the commit. – All staged files were added. – A ‘git status’ reveals nothing new or different.

Slide 31

Slide 31

Essential Git For Developers ● 31 Commits do not carry a version # – Git doesn’t use numbers like 1, 2, 3… – Instead uses hashes like 6e7e6999c879f460b5e1d7e29ffe9907062ec20a

Slide 32

Slide 32

Essential Git For Developers ● 32 Working in ‘master’ is bad. – Should not be working in the ‘master’ branch. – ‘master’ should be pristine version. ● Most bug free. ● Tested ● Same as “Production”

Slide 33

Slide 33

Essential Git For Developers ● 33 git branch – Shows a list of existing branches. – The * indicates active branch.

Slide 34

Slide 34

Essential Git For Developers 34 ● git branch {name} {branch} ● Or git checkout -b {name} {branch} – Creates new branch. – Checkout -b checks out after creation. – Below we create a ‘development’ branch. – New branch has same state as active/specified branch.

Slide 35

Slide 35

Essential Git For Developers ● 35 git checkout {name} – Include “-b” flag to create new branch. – Switches to a specified branch. – Branches carry own state. – In file browser file contents different.

Slide 36

Slide 36

Essential Git For Developers ● 36 What if? – A file has been edited, but not committed. – We are in ‘development’ branch. – What if we ‘git checkout master’?

Slide 37

Slide 37

Essential Git For Developers ● Change branch with uncommitted files – Merges uncommitted content on checkout. ● 37 Whether ‘staged’ or not. – Does NOT merge over newly created files. (changes only) – Conflicts get exciting. (Not covered in this talk.)

Slide 38

Slide 38

Essential Git For Developers ● File not actually changed – On ‘git checkout development’ and commit: ● File in development carries edit committed. ● File in master is reset, even though merged previously. master 38 development

Slide 39

Slide 39

Essential Git For Developers ● But if commit done first – Commit only done on active branch. – Master branch is unchanged. (‘git log’ shown below) – Master files do not contain merged changes. master 39 development

Slide 40

Slide 40

Essential Git For Developers ● 40 git merge {branch} – Git merges specified branch into active branch. – We merge change from development to master. ● ‘git checkout master’ ● ‘git merge development’

Slide 41

Slide 41

Essential Git For Developers ● What are “fast forward” commits? – 41 Merges individual commits into flow as if a checkout never occurred.

Slide 42

Slide 42

Essential Git For Developers ● Ignoring – – 42 We can exclude: ● Files ● Folders ● Config files with passwords ! ! ! Simply add excluded content to the file ‘.gitignore’.

Slide 43

Slide 43

Essential Git For Developers ● Typical branches for teams – 43 Conventions: ● Testing, Staging and Master branches off limits but public. ● Development public, public to all. ● {user}-development branches local and private.

Slide 44

Slide 44

Essential Git For Developers ● Typical rules for branch usage – No code leaves {user}-development unless finished and stable. ● – 44 Developers merge to development branch…period! Do NOT merge conflicts into any public branch.

Slide 45

Slide 45

Essential Git For Developers ● Commit procedure (origin pull/merge/push) – Before merging changes to public development: ● ‘git checkout development’ ● ‘git pull origin development’ Should be no conflicts. ‘git checkout {user}-development’ – ● ● ‘git merge development’ Fix conflicts ‘git checkout development’ – ● 45 ● ‘git merge {user}-development’ ● ‘git push origin development’

Slide 46

Slide 46

Essential Git For Developers ● Public and Private branches – – 46 Typically {user}-development branches remain private. ● The team is not aware of commits done there. ● Frequent commits encouraged. Development, Staging, and Master are public and entire team can view state/commits. ● All developers can merge to development. ● Only authorized people can merge to staging or master.

Slide 47

Slide 47

Essential Git For Developers ● Team Developer workflow – 47 Git is ideal for team development

Slide 48

Slide 48

Essential Git For Developers ● Team Developer workflow (private) – Project/ticket assigned, create branch ● ‘git checkout development’ ● ‘git branch {user}-development’ or ‘git checkout -b {user}-development’ – Start coding. – Commit often. Project assigned 48 Checkout dev. to private {user}-development

Slide 49

Slide 49

Essential Git For Developers ● Team Developer workflow (private) – – Regularly commit code. ● ‘git add {filename}’ X each file ● ‘git commit -m {commit message}’ Regularly pull from origin. ● ‘git checkout development’ followed by ‘git pull origin development’ ● ‘git checkout {user}-development’ followed by ‘git merge development’ Project assigned 49 Checkout dev. to private {user}-development Coding Multiple commits

Slide 50

Slide 50

Essential Git For Developers ● Team Developer workflow (private) – 50 Development completed, ready for QA testing. ● ‘git checkout development’ ● ‘git pull origin development’ should be no conflicts. ● ‘git merge {user}-development’ should be no conflicts. Project assigned Checkout dev. to private {user}-development Tests pass Merge changes to development Coding Multiple commits

Slide 51

Slide 51

Essential Git For Developers ● Team QA workflow (public) – Testing done in development branch. – Failed → developer picks up on {user}-development. – Bug fixed → re-push to development. Return to developer flow no Testing assigned 51 Testing from development Test passed

Slide 52

Slide 52

Essential Git For Developers ● Team QA workflow (public) – Testing done in development branch. – Success → merge to staging ● ‘git checkout staging’ ● ‘git pull origin staging’ ● ‘git merge development’ ● ‘git push origin staging’ Return to developer flow no Testing assigned 52 Testing from development Test passed yes Merge to staging

Slide 53

Slide 53

Essential Git For Developers ● Team Deployment Mgr. workflow (public) – Regression testing done in staging branch. – Testing failed: ● – ‘git branch -b {tempname}-staging’ Code to fix bug ● ‘git add {files}’ ● ‘git commit -m {message}’ Temp branch created no Deploy assigned 53 Regression testing In staging Test passed

Slide 54

Slide 54

Essential Git For Developers ● Team Deployment Mgr. workflow (public) – Send fix back for regression testing done in staging branch. ● ‘git merge staging’ just to check for conflicts. ● ‘git checkout staging’ ● ‘git merge {tempname}-staging’ Bug fixed Temp branch created no Deploy assigned 54 Regression testing In staging Test passed

Slide 55

Slide 55

Essential Git For Developers ● Team Deployment Mgr. workflow (public) – If regression tests pass: ● ‘git merge master’ in case of conflicts ● ‘git checkout master’ then ‘git pull origin master’ ● ‘git merge staging’ Bug fixed Temp branch created no Deploy assigned 55 Regression testing In staging Test passed yes Merge to master

Slide 56

Slide 56

Essential Git For Developers ● Team Deployment Mgr. workflow (public) – All is good, create annotated tag. ● ‘git tag -a v1.0 -m ‘{message}’ (Note: ‘git tag’ lists all tags.) Bug fixed Temp branch created no Deploy assigned Regression testing In staging Test passed yes Tag created 56 Merge to master

Slide 57

Slide 57

Essential Git For Developers ● Single Developer workflow (small project) – Pretty similar, but no staging. – Note: Still use {user}-development per task/project/ticket. Project assigned 57 {user}-development Test passed Merge to development Tag created Merge to master Test passed

Slide 58

Slide 58

Essential Git For Developers ● Tools – gitk – gitx – git-cola – SmartGit – GitEye – TortoiseGit – IDE ● Eclipse Zend Studio – Aptana PHPStorm – ● ● 58 etc.

Slide 59

Slide 59

Essential Git For Developers ● 59 github.com – Great place to share code. – Promotes collaboration – API enhances connectivity and use – Awesome plugins – Easy to use

Slide 60

Slide 60

Essential Git For Developers ● 60 github – how to clone locally – Standard ‘git clone’ command – Now have a clone local to work on. – Follow workflow as shown earlier.

Slide 61

Slide 61

Essential Git For Developers ● 61 Conclusion – Always use source control!!! – Git is an easy solution, just ‘git init’. – Plan a workflow and stick with it…ALWAYS! – 3rd party repositories = backed up – Git easy to connect to from anywhere. – Love iteration!

Slide 62

Slide 62

Essential Git For Developers ● 62 Resources – http://nvie.com/posts/a-successful-git-branching-model/ – http://github.com – http://training.github.com/ – https://bitbucket.org/ – http://git-scm.com

Slide 63

Slide 63

● Thank you! Essential Git For Developers Adam Culp http://www.geekyboy.com http://RunGeekRadio.com Twitter @adamculp https://joind.in/14744 Questions?