Slide 1

Slide 1 text

Version control (and continuous deployment) Jan Henckens (@jannemans) - PHP Leuven - March 2015 1

Slide 2

Slide 2 text

Me & Work • web developer @ statik.be • Expression Engine • Freelance WordPress developer • WordPress.org core contributor Jan Henckens (@jannemans) - PHP Leuven - March 2015 2

Slide 3

Slide 3 text

Have you ever: Accidentally removed to file over FTP? Jan Henckens (@jannemans) - PHP Leuven - March 2015 3

Slide 4

Slide 4 text

Have you ever: Uploaded changes, broke the site and now what? Jan Henckens (@jannemans) - PHP Leuven - March 2015 4

Slide 5

Slide 5 text

Have you ever: Renamed a file to .old as a backup? Jan Henckens (@jannemans) - PHP Leuven - March 2015 5

Slide 6

Slide 6 text

Have you ever: Noticed a page was broken and your last change was a month ago? Jan Henckens (@jannemans) - PHP Leuven - March 2015 6

Slide 7

Slide 7 text

Jan Henckens (@jannemans) - PHP Leuven - March 2015 7

Slide 8

Slide 8 text

! ! FTP " ! Jan Henckens (@jannemans) - PHP Leuven - March 2015 8

Slide 9

Slide 9 text

Version control to the rescue Jan Henckens (@jannemans) - PHP Leuven - March 2015 9

Slide 10

Slide 10 text

Version control (and continuous deployment) Jan Henckens (@jannemans) - PHP Leuven - March 2015 10

Slide 11

Slide 11 text

What is version control? • Keep track of certain files and changes to those files • Save the state of files in a historical timeline (repository) • What, When, Why, W ho Subversion, Git, Perforce, IBM Clearcase, Mercurial, ... Jan Henckens (@jannemans) - PHP Leuven - March 2015 11

Slide 12

Slide 12 text

Git “Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.” Distributed? Works offline, several users have their own separate file history (these can later on be merged) Jan Henckens (@jannemans) - PHP Leuven - March 2015 12

Slide 13

Slide 13 text

Getting started Start tracking files in a directory with git init: git init Git automatically creates a (hidden) .git folder containing: • Meta information • The entire history Jan Henckens (@jannemans) - PHP Leuven - March 2015 13

Slide 14

Slide 14 text

Jan Henckens (@jannemans) - PHP Leuven - March 2015 14

Slide 15

Slide 15 text

Staging and committing To save changes to git's history: 1) Select which files you want to save using git add → staging 2) Actually save them using git commit → committing Jan Henckens (@jannemans) - PHP Leuven - March 2015 15

Slide 16

Slide 16 text

Staging & unstaging • Stage all files > git add . • Stage all .js files > git add *.js • Stage an entire subfolder > git add subfolder/ • Deletions are not staged by default > git add -u Jan Henckens (@jannemans) - PHP Leuven - March 2015 16

Slide 17

Slide 17 text

Jan Henckens (@jannemans) - PHP Leuven - March 2015 17

Slide 18

Slide 18 text

Don't be afraid of commit(ment) • Commit early, commit often • Write clean commit messages • Small changes per commit • One bug fix per commit git commit -m “message” Jan Henckens (@jannemans) - PHP Leuven - March 2015 18

Slide 19

Slide 19 text

Jan Henckens (@jannemans) - PHP Leuven - March 2015 19

Slide 20

Slide 20 text

Reset & Stash Unstage all + discard all changes => git reset HEAD --hard The entire working directory will go back to the last committed state, cannot be undone Unstage all + stash all changes => git stash The entire working directory will go back to the last committed state, can be undone as the state is saved in a stash. Jan Henckens (@jannemans) - PHP Leuven - March 2015 20

Slide 21

Slide 21 text

Checking for changes • git status • git diff filename Jan Henckens (@jannemans) - PHP Leuven - March 2015 21

Slide 22

Slide 22 text

Jan Henckens (@jannemans) - PHP Leuven - March 2015 22

Slide 23

Slide 23 text

Checking history • git log Jan Henckens (@jannemans) - PHP Leuven - March 2015 23

Slide 24

Slide 24 text

Jan Henckens (@jannemans) - PHP Leuven - March 2015 24

Slide 25

Slide 25 text

Checking history • git log but that's not the most helpful. • git log --oneline Jan Henckens (@jannemans) - PHP Leuven - March 2015 25

Slide 26

Slide 26 text

Jan Henckens (@jannemans) - PHP Leuven - March 2015 26

Slide 27

Slide 27 text

Checking history • git log but that's a mess... • git log --oneline makes it a bit better • git log --graph --pretty Jan Henckens (@jannemans) - PHP Leuven - March 2015 27

Slide 28

Slide 28 text

Checking history • git log but that's a mess... • git log --oneline makes it a bit better • git log --graph --pretty=format:'%Cred%h%Creset - %C(bold yellow)%d%Creset %s %Cgreen(%cr) %C(cursive blue)<%an>%Creset' --abbrev-commit (via @bramus) (might want to alias that) Jan Henckens (@jannemans) - PHP Leuven - March 2015 28

Slide 29

Slide 29 text

Jan Henckens (@jannemans) - PHP Leuven - March 2015 29

Slide 30

Slide 30 text

Basics summary • git init • git add • git commit • git status • git reset • git log Jan Henckens (@jannemans) - PHP Leuven - March 2015 30

Slide 31

Slide 31 text

Branches! ! • historical timeline of your changes • more than one branche can exists (lots more on that later) • default branch is master Jan Henckens (@jannemans) - PHP Leuven - March 2015 31

Slide 32

Slide 32 text

Where's your HEAD at? The HEAD points to the currently checked out (= active) revision (= commit) To move the HEAD you checkout a revision: git checkout revision Jan Henckens (@jannemans) - PHP Leuven - March 2015 32

Slide 33

Slide 33 text

Jan Henckens (@jannemans) - PHP Leuven - March 2015 33

Slide 34

Slide 34 text

Basics • git branch experiment => creates a branch called 'experiment' • git checkout experiment => moves HEAD to the new branch • Changes are committed on the active branch Jan Henckens (@jannemans) - PHP Leuven - March 2015 34

Slide 35

Slide 35 text

Merging commits to other branches • Checkout the branch to which you want to merge: git checkout master • git merge expermiment will merge the changes from experiment with master • A merge commit is created Jan Henckens (@jannemans) - PHP Leuven - March 2015 35

Slide 36

Slide 36 text

Cleaning up • git branch -d experiment will remove the branch • If the branch hasn't been merged Git will yell at you about it (use -D instead of -d to overwrite that) Jan Henckens (@jannemans) - PHP Leuven - March 2015 36

Slide 37

Slide 37 text

Conflicts • When the same line has been edited on both branches => Merge conflict • Fix the conflict in a text editor, stage the file and commit • Pick the version from the branch you're on (HEAD): git checkout --ours conflictedfile • Pick the version from the branch your trying to merge into HEAD: git checkout --theirs conflictedfile Jan Henckens (@jannemans) - PHP Leuven - March 2015 37

Slide 38

Slide 38 text

Remotes ! • A repository can be synced with a server, in git this server is called a remote • A remote has a name (defaulting to origin) and a url • Multiple people can access the same remote • Multiple remotes are possible (distributed) • push / pull to keep the local and remote copy in sync Jan Henckens (@jannemans) - PHP Leuven - March 2015 38

Slide 39

Slide 39 text

Starting from scratch If a local copy of the repository doesn't exist yet: git clone [email protected]:laravel/framework.git • a local copy of the master branch will be created • includes the entire commit history Jan Henckens (@jannemans) - PHP Leuven - March 2015 39

Slide 40

Slide 40 text

Pushing exciting code to a new remote Add a new remote: git remote add origin [email protected]:janhenckens/ laravel-app.git Push your changes to the remote: git push origin master Jan Henckens (@jannemans) - PHP Leuven - March 2015 40

Slide 41

Slide 41 text

Jan Henckens (@jannemans) - PHP Leuven - March 2015 41

Slide 42

Slide 42 text

Collaboration • never1 develop straight on master • master should always be deployable, working code • commit early, commit often 1 Unless... Jan Henckens (@jannemans) - PHP Leuven - March 2015 42

Slide 43

Slide 43 text

Git flow Branching model for working with features, releases and hotfixes • master/develop • features (starts from develop) • releases (merges develop into master) • hotfix (starts from master) Jan Henckens (@jannemans) - PHP Leuven - March 2015 43

Slide 44

Slide 44 text

A few more tricks • .gitignore for files you don't want to track • Be careful when committing config files (credentials, api keys, etc) • ‘git blame’ - see who broke things and when Jan Henckens (@jannemans) - PHP Leuven - March 2015 44

Slide 45

Slide 45 text

Github.com • Embraced by the open source community • Github != git • Forking and pull requests • Unlimited free public repo’s • Mac app • Github != git Jan Henckens (@jannemans) - PHP Leuven - March 2015 45

Slide 46

Slide 46 text

try.github.io Jan Henckens (@jannemans) - PHP Leuven - March 2015 46

Slide 47

Slide 47 text

Learning git • Start by using git locally for your own projects • You don’t have to know all this by heart • You don’t have to use the command line • Stick with it, it’s not that hard Jan Henckens (@jannemans) - PHP Leuven - March 2015 47

Slide 48

Slide 48 text

Apps • Sourcetree (Windows, Mac OS, Linux) • Tower (Mac OS) • Github (Windows, Mac OS, Linux) Jan Henckens (@jannemans) - PHP Leuven - March 2015 48

Slide 49

Slide 49 text

Continuous deployment Jan Henckens (@jannemans) - PHP Leuven - March 2015 49

Slide 50

Slide 50 text

git push > deploy > ! • Know when your changes are live • Roll back when something breaks • Everyone can deploy • NO MORE FTP Jan Henckens (@jannemans) - PHP Leuven - March 2015 50

Slide 51

Slide 51 text

How? • Connect your repo with one of these service • Connect the service to your server (ssh, sftp) • Deploy all the things But but but • Database changes? Jan Henckens (@jannemans) - PHP Leuven - March 2015 51

Slide 52

Slide 52 text

Services • beanstalkapp.com (hosting + deploy) • dploy.io (hosting + deploy) • deployhq.com (deploy) • codeship.io (ci + deploy) Or you could roll your own Jan Henckens (@jannemans) - PHP Leuven - March 2015 52

Slide 53

Slide 53 text

Summary 1) Use git, always. Make it a habit. 2) Push > deploy 3) ! Jan Henckens (@jannemans) - PHP Leuven - March 2015 53

Slide 54

Slide 54 text

? Jan Henckens (@jannemans) - PHP Leuven - March 2015 54

Slide 55

Slide 55 text

feedback -> https://joind.in/14311 Jan Henckens (@jannemans) - PHP Leuven - March 2015 55

Slide 56

Slide 56 text

⌘ + Q twitter.com/jannemans [email protected] github.com/janhenckens Jan Henckens (@jannemans) - PHP Leuven - March 2015 56