Foundation for Web Developers

Chris Kemper and Ian Oxley FOUNDATION VERSION CONTROL FOR WEB DEVELOPERS Copyright © 2012 by Chris Kemper and Ian Oxley This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher's location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law. ISBN 978-1-4302-3972-7 ISBN 978-1-4302-3973-4 (eBook) Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, or images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected] or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales. Any or other supplementary materials referenced by the author in this text are available to readers at www.apress.com. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/. Credits

President and Publisher: Coordinating Editor: Paul Manning Tracy Brown Lead Editor: Copy Editors: Louise Corrigan Elizabeth Berry, Tiffany Taylor Technical Reviewer: Compositor: Tom Barker Mary Sudul Editorial Board: Indexer: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary SPi Global Cornell, Morgan Ertel, Jonathan Gennick, Jonathan Artist: Hassell, Robert Hutchinson, Michelle Lowman, James SPi Global Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Cover Image Artist: Shakeshaft, Gwenan Spearing, Matt Wade, Tom Welsh Corné van Dooren Cover Designer: Anna Ishchenko

ii To my family, friends, and Lauren —Chris Kemper

To Rachel, Stewart, and Adam —Ian Oxley Contents at a Glance

About the Authors ...... xvii About the Technical Reviewer...... xviii About the Cover Image Artist ...... xix Acknowledgments ...... xx Introduction...... xxi

Chapter 1: Are You Set Up? Creating a Basic Development Environment...... 1

Chapter 2: In the Beginning There Were Just Files...... 25

Chapter 3: Meet the Current Players in the Game: SVN, , and ...... 33

Chapter 4: The Benefits of Version Control ...... 75

Chapter 5: I Don’t Like Terminal; Which Programs Can I Use? ...... 91

Chapter 6: You Mean I Can Version Control Everything?! ...... 175

Chapter 7: What Can My Life Easier? ...... 193

Chapter 8: I Have a Conflict: What Can I Do? ...... 221

Chapter 9: I’m Feeling Brave; Can I Do This With Terminal? ...... 243

Chapter 10: How to Create a Server...... 279

Chapter 11: I Don’t Have a Server, What Other Options Do I Have? ...... 293

Chapter 12: Why Branching Is Great ...... 323

Chapter 13: Hooks, and Why They Can Be Useful ...... 341

Chapter 14: Upgrading from CVS and Converting Repositories ...... 359

Appendix: Terminal Commands...... 369

Index ...... 381

iv Contents

About the Author ...... xvii About the Technical Reviewer...... xviii About the Cover Image Artist ...... xix Acknowledgments ...... xx Introduction...... xxi

Chapter 1: HTML5 Page Structures ...... 1 ...... 2 Getting the package...... 2 Getting your install on ...... 3 Mac...... 11 Getting the package...... 12 Setting up...... 13 MAMP Pro ...... 16 Ubuntu ...... 17 Package manager...... 18 Terminal method...... 19 Summary ...... 23

Chapter 2: In the Beginning There Were Just Files...... 25 What is version control?...... 25 The earlier version control solutions...... 26 SCCS...... 27 RCS ...... 27 PVCS ...... 27 CVS ...... 28 ClearCase...... 28 VSS...... 29 ...... 30 AccuRev ...... 30 BitKeeper ...... 31 Summary ...... 31

v CONTENTS

Chapter 3: Meet the Current Players in the Game: SVN, Git, and Mercurial ...... 33 The differences between then and now...... 33 An introduction to the lead players: SVN, Git, and Mercurial ...... 34 What’s the difference between them and why should I care? ...... 34 (SVN)...... 34 Mercurial ...... 35 Git ...... 36 Where to install and usr/local...... 37 Compiling software ...... 38 The process of SVN and setting it up ...... 40 Setting up on Windows ...... 42 Setting up on Mac...... 43 Setting up on Ubuntu ...... 46 Using a real application to work with SVN ...... 50 How Mercurial does the job and getting it set up...... 55 Setting up on Windows ...... 56 Setting up on Mac...... 58 Setting up on Ubuntu ...... 59 Compiling from source...... 61 How Git is a little bit different ...... 63 Setting up on Windows ...... 64 Setting up on Mac...... 70 Setting up on Ubuntu ...... 71 Compiling from source...... 73 Summary ...... 74

Chapter 4: The Benefits of Version Control ...... 75 Having a backup of your files...... 75 Naming conventions ...... 75 USB drives...... 76 External hard drives...... 76 Dropbox ...... 77 Backup summary ...... 77 Collaborative projects ...... 77 vi CONTENTS

The ability to work on different machines...... 78 A full history of changes...... 79 Safety from deleted files ...... 79 Going back to a previous version ...... 80 Checking for differences between files ...... 80 Dealing with indecisive clients ...... 80 Comments ...... 82 Using comments to deal with bugs ...... 82 The drawbacks of e-mail...... 83 Giving clients confidence in you...... 84 Working on projects and files ...... 85 Going local...... 85 Merging...... 86 Branching...... 87 Bridging ...... 89 Summary ...... 90

Chapter 5: I Don’t Like Terminal; Which Programs Can I Use? ...... 91 SVN ...... 91 Windows ...... 91 Mac ...... 107 ...... 123 SVN Summary ...... 131 Mercurial...... 131 Windows ...... 131 Mac ...... 144 Mercurial summary ...... 156 Git...... 156 Windows ...... 156 Mac ...... 163 Git summary ...... 172 Summary ...... 173

vii CONTENTS

Chapter 6: You Mean I Can Version Control Everything?! ...... 175 Why version control isn’t the solution for some files ...... 175 Cloud storage ...... 176 zovo ...... 176 Amazon Cloud Drive...... 178 ZumoDrive ...... 180 Dropbox ...... 182 Visual versioning ...... 185 PixelNovel...... 185 Adobe Drive 3 ...... 185 Zoom...... 186 Document management ...... 187 Microsoft SharePoint ...... 187 HyperOffice...... 188 MagnetSVN ...... 189 Google Docs ...... 190 Summary ...... 191

Chapter 7: What Can Make My Life Easier? ...... 193 Windows editors ...... 194 Notepad++ ...... 194 Visual Studio...... 195 Mac editors...... 195 TextMate...... 196 Coda ...... 199 BBEdit...... 200 Cross platform ...... 201 Eclipse/Aptana...... 202 NetBeans ...... 210 SVN ...... 210 Mercurial ...... 211 Git ...... 212 Adding and committing ...... 213

viii CONTENTS

Sublime Text 2 ...... 215 Mercurial ...... 216 SVN ...... 217 Git ...... 217 Summary ...... 219

Chapter 8: I Have a Conflict: What Can I Do? ...... 221 Manual conflict management ...... 221 SVN ...... 222 Mercurial ...... 223 Git ...... 224 Configuring a tool for use ...... 225 GUIs ...... 225 Windows ...... 225 Mac ...... 227 Linux ...... 234 Cross-platform ...... 235 Summary ...... 241

Chapter 9: I’m Feeling Brave; Can I Do This WithTerminal? ...... 243 SVN ...... 244 .bash_profile on Mac or Linux...... 244 SVN merge ...... 246 Setting up on Windows ...... 248 Global options...... 249 SVN checkout ...... 250 svn status...... 251 Update ...... 253 Specific revisions ...... 254 svn export ...... 254 svn help ...... 255 svn log ...... 255 svn import ...... 255 Conflicts, and how to solve them ...... 256 Reverting changes...... 258

ix CONTENTS

Deleting, moving, and renaming files...... 260 svn add * (multiple files)...... 260 SVN Summary ...... 260 Mercurial...... 260 Configuration ...... 261 Some global options ...... 263 Creating and cloning repositories ...... 263 Basic file operations...... 264 Going remote ...... 267 Mercurial summary ...... 268 Git...... 268 Setting up merge tools...... 268 Creating a repository ...... 270 Cloning a repository...... 270 Basic commands ...... 270 git log ...... 272 Reverting unstaged changes ...... 273 Moving and renaming files...... 274 Reverting to previous changes ...... 274 Tagging...... 275 Pulling changes ...... 275 Pushing changes ...... 275 Git summary ...... 276 Chapter summary ...... 276

Chapter 10: How to Create a Server...... 279 Setting up the server ...... 280 Domain names and DNS records ...... 280 A few good hosting providers ...... 281 Linode ...... 281 Media Temple ...... 282 SVN ...... 282 Getting started ...... 282 Managing users ...... 283 x CONTENTS

Integrating with Apache ...... 283 You’re up and running! ...... 285 Mercurial...... 285 Getting started ...... 285 Managing users ...... 286 Integrating with Apache ...... 287 Git...... 289 Getting started ...... 289 Gaining access to the server ...... 290 Cloning the repository...... 291 Summary ...... 292

Chapter 11: I Don’t Have a Server, What Other Options Do I Have? ...... 293 Beanstalk...... 293 Features...... 294 Support ...... 296 Pricing...... 296 To sum up Beanstalk ...... 297 Springloops...... 297 Features...... 298 Pricing...... 300 To sum up Springloops...... 301 Unfuddle ...... 301 Features...... 301 Pricing...... 303 To sum up Unfuddle ...... 304 Google Project Hosting...... 304 Features...... 305 To sum up Google Project Hosting...... 306 Assembla...... 306 Features...... 306 Pricing...... 308 To sum up Assembla ...... 308

xi CONTENTS

XP-Dev ...... 308 Features...... 308 Pricing...... 310 To sum up XP-Dev ...... 310 Codesion ...... 310 Features...... 310 Support ...... 311 Pricing...... 311 To sum up Codesion...... 312 ...... 312 Features...... 312 Pricing...... 314 To sum up BitBucket...... 314 CodebaseHQ ...... 315 Features...... 315 Pricing...... 316 To sum up CodebaseHQ ...... 317 GitHub...... 317 Features...... 318 Pricing...... 320 To sum up GitHub...... 320 I’m still not feeling these services, what else is available?...... 321 Summary ...... 321

Chapter 12: Why Branching Is Great ...... 323 What is a branch?...... 323 When can I use branches? ...... 324 Release branches...... 324 Feature branches...... 324 How do I create a branch? ...... 325 SVN ...... 325 Git ...... 325 Mercurial ...... 326

xii CONTENTS

A worked example ...... 328 SVN ...... 328 Git ...... 331 Mercurial ...... 337 Summary ...... 339

Chapter 13: Hooks, and Why They Can Be Useful ...... 341 SVN Hooks ...... 341 start- ...... 341 pre-commit...... 342 post-commit ...... 342 pre-revprop-change ...... 342 post-revprop-change...... 343 Git Hooks...... 343 pre-commit...... 343 commit-msg ...... 343 post-commit ...... 344 prepare-commit-msg...... 344 pre-rebase ...... 344 post-receive ...... 344 post-update...... 344 update...... 344 pre-applypatch ...... 345 applypatch-msg ...... 345 Mercurial Hooks...... 345 prechangegroup...... 345 changegroup...... 345 pretxnchangegroup...... 345 preoutgoing...... 346 outgoing ...... 346 incoming ...... 346 precommit ...... 346 commit ...... 346 pretxncommit ...... 346

xiii CONTENTS

preupdate...... 346 update...... 346 pretag...... 346 tag...... 347 Enabling Hooks...... 347 Enabling Hooks in SVN ...... 347 Enabling Hooks in Git ...... 348 Enabling hooks in Mercurial...... 348 Defining multiple actions for a Hook ...... 349 Hooks in action ...... 349 Preventing empty commit messages...... 349 Checking a commit message contains a bug ticket number...... 352 Running JSLint tests before committing changes...... 354 Sending e-mail notifications of commits ...... 355 Summary ...... 358

Chapter 14: Upgrading from CVS and Converting Repositories ...... 359 CVS ...... 360 Changing to SVN ...... 360 Turning CVS into Mercurial...... 361 CVS to Git...... 362 CVS migration summary...... 363 SVN ...... 364 SVN to Mercurial...... 364 SVN to Git...... 365 Mercurial to Git ...... 366 Git to Mercurial ...... 367 Mercurial or Git to SVN...... 368 Summary ...... 368

Appendix: Terminal Commands...... 369 SVN ...... 369 Checking out a repository ...... 369 Adding files ...... 370 Committing files ...... 370 xiv CONTENTS

Log...... 370 Updating ...... 370 Reverting ...... 371 Deleting...... 371 Cleanup ...... 371 Status...... 371 Performing a ...... 371 Branching...... 372 Switch to a different branch ...... 372 Merging...... 372 Resolving conflicts ...... 372 Moving or renaming files...... 373 Blame...... 373 Help ...... 373 Mercurial...... 373 Creating a repository ...... 373 Status...... 373 Checking out/updating ...... 374 Pushing changes ...... 374 Pulling changes ...... 374 Committing changes ...... 374 Adding files ...... 375 Removing files ...... 375 Adding and removing...... 375 Blame...... 375 Resolving ...... 375 Moving or renaming files...... 376 Reverting changes...... 376 Cloning repositories ...... 376 Merge...... 376 Help ...... 377 Branching...... 377 Git...... 377 Adding files ...... 377

xv CONTENTS

Creating a repository ...... 377 Cloning a repository...... 377 Checking out branches ...... 378 Branching...... 378 Help ...... 378 Committing changes ...... 378 Log...... 378 Status...... 379 Merging branches ...... 379 Moving or renaming files...... 379 Pulling changes ...... 379 Pushing changes ...... 379 Removing files ...... 380

Index ...... 381

xvi About the Authors

Chris Kemperr was born and bred in the North of England. Growing up taking computers apart and putting them back together, it’s no surprise he got into web development in his early teens. Graduating in 2008, Chris secured a job at one of the leading web design agencies in the North East, building awesome Drupal- powered websites for some big names. Moonlighting as a freelance web developer, Chris always stays on the cutting edge of web development—which means he also has a lot of big ideas and not enough time to develop them. On the rare occasion Chris isn’t on his MacBook, he can be found exploring the realms of Skyrim and beyond, travelling the country for various web conferences, Photo credit: Graham Smith and drinking fine ales. You can catch Chris on Twitter at @ChrisDKemper or by checking out his personal site at www.chrisdkemper.co.uk.

Ian Oxley is a web developer who’s been writing front-end and back-end code professionally since 2004. He’s based in Newcastle-upon-Tyne, England, and can often be found attending, and on occasion speaking at, local user groups and meetups. When he’s not programming, Ian enjoys playing the guitar, photography, badminton, and spending time with his wife and family.

xvii About the Technical Reviewer

Tom Barker is a software engineer, solutions architect, and technical manager with over 13 years of experience working with ActionScript, JavaScript, Perl, PHP, and the Microsoft .Net Framework. Currently he is the manager of web development at Comcast Interactive Media where he leads the craftsmen responsible for www.xfinity.com and www.xfinitytv.com. He is also an adjunct professor at Philadelphia University, where he has been teaching undergraduate and graduate courses on web development since 2003. When not working, teaching, or writing, Tom likes to spend time with his family, read, and play video games until very early in the morning.

xviii About the Cover Image Artist

Corné van Dooren designed the front cover image for this book. After taking a brief hiatus from friends of ED to create a new design for the Foundation series, Corné worked at combining technological and organic forms with the results now appearing on this and other book covers. Corné spent his childhood drawing on everything at hand, and then he began exploring the infinite world of multimedia—His journey of discovery hasn’t stopped since! His mantra has always been, “The only limit to multimedia is the imagination,” a saying that keeps him constantly moving forward. Corné works for many international clients, writes features for multimedia magazines, reviews and tests software, authors multimedia studies, and works on many other friends of ED books. You can see more of his work and contact him through his website at: www.cornevandooren.com.

xix Acknowledgments

To all my friends who have offered kind words to me at times of stress, I thank you. My mam practically bullied her way in here, but I love her all the same! My brother Kane also gets a mention, because of his support, and helping me suck less at Battlefield. To my dad, who would quite happily listen to me talk about the book without any knowledge of what the hell I was talking about, thanks! Phil Sherry gets a big mention, as he’s always offered words of advice (although most of them look like “$!#@) and he got me in touch with the right people to start this whole process off. I owe you a beer, my good man. Craig Tweedy gets a mention, mainly because he’s Craig Tweedy. Ian Oxley also has to get a shout out, since he came and helped me out and produced two cracking chapters for the book in the process. Cheers, man! Finally, I have to thank my partner, Lauren Thompson, for putting up with all the late nights, stress, and pointless conversation, as well as even helping me write and proof various parts of the book; you get hugs and a Nandos.

xx Introduction

Since you happen to be reading through the opening pages of Foundation Version Control for Web Developers then it’s safe to say you have some interest in finding out a little bit more about version control. It may be true that this new interest isn’t actually your own, but instead comes from a friend, colleague, or employer who thinks getting a bit more knowledge on the subject would be beneficial to you. Either way, throughout the course of this book you’ll be walked through the basics of version control using Subversion (SVN), Mercurial, and Git, getting to know each of them a little better as the chapters go on. I’ll cover setting up a development environment first, just in case you’re really new to the development scene, then the programs you can use for versioning your code (Chapter 5), and even how to dive into Terminal and all your versioning needs with commands (Chapter 9). Don’t worry if any of this seems daunting. As you progress through the chapters you’ll feel your confidence in version control growing, and before long you’ll be a versioning master!

Who this book is for Foundation Version Control for Web Developers is a book to help those with little to no knowledge of version control get a leg up on one of key components of modern-day development. Whether you are just getting started in the field, a pro who hasn’t been versioning code until now, or even a designer wanting to expand your knowledge of the field, this book is for you. And whether you’re a freelancer or a full-time agency worker, you’ll always have a need for versioning your code. Knowing more about your chosen field is always a good thing under any circumstance.

How this book is structured As mentioned previously, I’ll start off by showing you how to get a development environment of PHP and MySQL set up on your Windows, Mac, or Ubuntu machine. In Chapter 2, you’ll be learning about what version control actually is and why it’s so important, as well as some history on where the books featured systems came from. Then in Chapter 3, it’ll be time to get SVN, Mercurial, and Git installed on your machine, to get you ready for the rest of the book and versioning your code in general. Chapter 4 will introduce the real benefits of version control, showing many other potential situations where having access to some kind of versioning system would make life so much easier. Next, you’ll dive into the world of programs you can utilize to harness the power of version control, without having to go near a Terminal window! The level will come down a little in Chapter 6, where you’ll learn that you shouldn’t version everything using these systems, and I’ll show you some alternatives, just in case you have a need for it. In Chapter 7, I’ll try and make your life a little easier by showing how you can integrate version control into some of your favorite development applications and take you on a tour of the features for those that already boost integration. Conflict management is the topic of Chapter 8, showing how to resolve the pesky conflicts that you’ll get from time to time, both in the code and using applications. The applications will go away in Chapter 9, where I’ll take you into Terminal and show how to get your version control on with just the commands you need to get started! With your confidence from the previous chapter, it’ll be time to create your own server, which you can use to store all of those remote repositories

xxi INTRODUCTION

you’ll want to be using! If that isn’t your thing, then Chapter 11 will be just right for you, in which I’ll cover some of the packages out there that have done the hard work for you and created some great repository hosting services, with all the trimmings. In Chapter 12, Ian Oxley will be taking you through why branching is so awesome, and how to go about utilizing those awesome features. Ian will then lead you into Chapter 14, detailing what commit hooks are and how they can be really useful in your versioning workflow. Finally, in the last chapter in the book I’ll bring you in for the close, with a chapter on upgrading your CVS repository, if you happen to have one, and how to migrate from SVN to Git or Mercurial, if you fancy a change.

What will I need? As far the book goes, as long as you have either an up-to-date Mac, PC, or Ubuntu installation, you’ll have no problem using these great applications and bettering your version control knowledge. The only other thing you’ll need is a desire to learn some awesome new techniques and to make sure that your code stays safe and is backed up.

Contacting Chris Kemper Chris Kemper can be contacted at either [email protected] or through his Twitter account @chrisdkemper.

xxii