Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Version control basics & continuous deployment

Version control basics & continuous deployment

Talk given at the PHP Leuven meetup group in March '15, at Kunstmaan.be

Jan Henckens

March 26, 2015
Tweet

More Decks by Jan Henckens

Other Decks in Technology

Transcript

  1. Me & Work • web developer @ statik.be • Expression

    Engine • Freelance WordPress developer • WordPress.org core contributor Jan Henckens (@jannemans) - PHP Leuven - March 2015 2
  2. Have you ever: Accidentally removed to file over FTP? Jan

    Henckens (@jannemans) - PHP Leuven - March 2015 3
  3. Have you ever: Uploaded changes, broke the site and now

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

    backup? Jan Henckens (@jannemans) - PHP Leuven - March 2015 5
  5. Have you ever: Noticed a page was broken and your

    last change was a month ago? Jan Henckens (@jannemans) - PHP Leuven - March 2015 6
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. Checking for changes • git status • git diff filename

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

    helpful. • git log --oneline Jan Henckens (@jannemans) - PHP Leuven - March 2015 25
  15. 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
  16. 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
  17. Basics summary • git init • git add • git

    commit • git status • git reset • git log Jan Henckens (@jannemans) - PHP Leuven - March 2015 30
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. Apps • Sourcetree (Windows, Mac OS, Linux) • Tower (Mac

    OS) • Github (Windows, Mac OS, Linux) Jan Henckens (@jannemans) - PHP Leuven - March 2015 48
  33. 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
  34. 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
  35. 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
  36. Summary 1) Use git, always. Make it a habit. 2)

    Push > deploy 3) ! Jan Henckens (@jannemans) - PHP Leuven - March 2015 53