Git (Get) it done right! Practical Applied Version Control for Drupal site developers Peter Chen - Stanford School of Engineering Technical Webmaster What is Version Control? (why would I need it...) • • • • Your filesystem goes through frequent changes. i.e. any website. From modified files to new files coming in and old ones being deleted. If you build any kind of application (CMS) and need to keep track of “versions” of the filesystem, you need version control. If you need to share coding responsibilities or maintenance of a codebase with another person, you need version control. If you want to look like a pro at managing your code, keep a consistent history (with ease), and feel comfortable handing it off to someone else, you need version control. Why aren't you using it today? (some convenient excuses...) • • • • • I’m getting by just fine without needing to share coding responsibilities with anyone else A bad experience with Subversion crashing scared me off. I only need to make occasional upgrades to my module/theme packages, and have only one or two css files that I edit. It’s too hard (and I’m not that good at command line) to get started. branching, merging, committing, clone, init, fetch, pull, push, origin, head? Augh! I have enough trouble remembering drush commands alone and when I see those code trees, I freak out. Types of Version Control Systems • • • Local only - keeps a local database of changes in your local machine filesystem. Centralized - (Subversion, CVS), require a connection to a central server and “checkout” Distributed - (Git, Mercurial) allow for local systems to be “mirrors” of the central repo. You don’t need to be connected to the central server to get work or commits done. What Git is and what it does • small piece of software principally written for Linux. http://git-scm.com/downloads (mac: macports, homebrew, googlecode) (windows: googlecode) tracks all your files in it’s database. You have to add and remove files into this tracking system and commit them. • • • • • a single .git directory at the top of your filesystem “watches” the changes going on and helps you deal with them at commit time. “git status” will tell you what has been added, removed, modified, etc. etc. When you make a “commit.” Git records a “snapshot” of changes to your filesystem and records an index number to it. You also write a brief message about the commit. Commit frequently! Where it all happens global settings are in ~/.gitconfig in each local .git folder, you can have a local config file. (On windows, this is usually in the default C:\Documents and Settings\$USER directory) • most activity happens locally. Frequent commits, additions, and changes. • When you’re ready to share your code with others in • your team, or want to get it ready for deployment to your servers, you can “push” it to your "remote" So how do I get a "remote" and how do I connect with it? SSH, GitHub, and remotes SSH - authenticated protocol for communicating between two machines by using a private/public key pair. You put the public key on the “shared repo” machine (github, codeplane, bitbucket. etc.) so that it recognized your machine when you talk to it and can move files and info back and forth. Github has an excellent starter guide that helps you get git installed and set up ssh keys on your machine (http://help.github.com/set-up-git-redirect) There are other 3rd party services like codeplane and bitbucket that you can set up a centralized repo with. But if you have a linux server or host somewhere, it can be relatively easy to set up a git remote • • Git started - working with git locally • invoke git init • now add files • now commit files • add? commit? What does it mean? • git status • git log • Congrats! We just made a git repo! Going the other way - grabbing a repo from a remote git clone - do this just initially protocols: ssh, http://, git:// Fine... now how do I do this with Drupal? How a remote gets involved Going from local to remote to a deployment (or production) system git fetch origin master o git pull origin master o git push origin master o Other important stuff to know The .gitignore file Other git commands that will become useful quite quickly checkout branch merge tag Tools that can make Git easier For Mac: GitHub for mac (http://mac.github.com/) SourceTree (http://www.sourcetreeapp.com/) For Windows: • • • • Git for Windows (http://msysgit.github.com/) TortoiseGit (http://code.google.com/p/tortoisegit/) If you develop with an IDE, most modern IDEs such as Eclipse, Netbeans, Komodo, XCode, Aptana, Zend all have plugins for GIT and other Version Control software Recommended practices on using this with Drupal • • • • have a .gitignore file add site/all/default (or wherever your settings.php file will reside) to it files directory should be ignored other automated files that aren’t part of your “code base” like the backup_migrate backups destination, security review report destination, etc. etc. Some Stanford sites specific recommendations • • • • • Yes, git is installed on afs. First, set up your ssh key in your afs home directory. You should probably exclude drupal core if your site is managed by sites.stanford.edu Thus git init at your sites directory have a .gitignore file at the sites directory exclude: sites/default* wherever your settings.php file is (if not sites/default) wherever your files, backup_migrate destination directory are. Some Stanford sites specific recommendations (2) Recommended workflow for a Stanford Site: 1. Set up ssh keypair in your afs home directory 2. Get your Drupal (group, dept) site in afs 3. Build your site locally, use git init to keep track of what you're doing (set .git and .gitignore up in your sites directory) 4. Set up your remote repo with a git hosting service and do your initial git push to get your code up there. 5. In afs, on your site, mv (do not delete) your original sites directory (recommend you back it to your home directory somewhere) and replace it with your git managed sites directory with git clone 6. Now restore settings.php from the original sites directory along with files and any other resources from the original sites directory that you still need. Good resources on practicing and doing more on git Github: http://github.com The ProGit book (A-press) but also online at http://git-scm.com/book http://gitimmersion.com - a great tutorial that will get you far and teach some great shortcuts. Thank You Slides will be available on http://drupalcamp.stanford.edu or email me at ptchen@stanford.edu