Share
Embed
Version control basics & continuous deployment
Talk given at the PHP Leuven meetup group in March '15, at Kunstmaan.be
Jan Henckens
March 26, 2015
Transcript
56 slides
-
Version control (and continuous deployment) Jan Henckens (@jannemans) - PHP
Leuven - March 2015 1 -
Me & Work • web developer @ statik.be • Expression
Engine • Freelance WordPress developer • WordPress.org core contributor Jan Henckens (@jannemans) - PHP Leuven - March 2015 2 -
Have you ever: Accidentally removed to file over FTP? Jan
Henckens (@jannemans) - PHP Leuven - March 2015 3 -
Have you ever: Uploaded changes, broke the site and now
what? Jan Henckens (@jannemans) - PHP Leuven - March 2015 4 -
Have you ever: Renamed a file to .old as a
backup? Jan Henckens (@jannemans) - PHP Leuven - March 2015 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 -
! ! FTP " ! Jan Henckens (@jannemans) - PHP
Leuven - March 2015 8 -
Version control to the rescue Jan Henckens (@jannemans) - PHP
Leuven - March 2015 9 -
Version control (and continuous deployment) Jan Henckens (@jannemans) - PHP
Leuven - March 2015 10 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
Checking for changes • git status • git diff filename
Jan Henckens (@jannemans) - PHP Leuven - March 2015 21 -
Checking history • git log Jan Henckens (@jannemans) - PHP
Leuven - March 2015 23 -
Checking history • git log but that's not the most
helpful. • git log --oneline Jan Henckens (@jannemans) - PHP Leuven - March 2015 25 -
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 -
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 -
Basics summary • git init • git add • git
commit • git status • git reset • git log Jan Henckens (@jannemans) - PHP Leuven - March 2015 30 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
Apps • Sourcetree (Windows, Mac OS, Linux) • Tower (Mac
OS) • Github (Windows, Mac OS, Linux) Jan Henckens (@jannemans) - PHP Leuven - March 2015 48 -
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 -
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 -
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 -
Summary 1) Use git, always. Make it a habit. 2)
Push > deploy 3) ! Jan Henckens (@jannemans) - PHP Leuven - March 2015 53 -
⌘ + Q twitter.com/jannemans [email protected] github.com/janhenckens Jan Henckens (@jannemans) -
PHP Leuven - March 2015 56