I'd like to be able to use git version control with Craft. Is this possible and if it is what is the best approach to ensure that all files/assets and the database are properly synced across local, staging and production servers?
Matt has a point, this is similar to What's the best practice for handling data migration and organization across development environments?, but that question focuses mainly on how to handle the database (definitely see those answers).
More generally speaking...
Set yourself up with Multi-Environment Configs in both
craft/config/db.php so that you don't have to be updating path settings every time you migrate data between environments.
Be sure to set some Environment-Specific Variables for the site URL and server path for each environment. That way you can use those variables in the control panel settings (the Site URL setting in the Setting → System → General and Assets paths in Settings → Assets → Your asset settings).
Here is a sample
return array( '*' => array( 'omitScriptNameInUrls' => true, 'usePathInfo'=> true, 'cpTrigger' => 'example-admin', 'allowAutoUpdates' => false, ), 'example.dev' => array( 'devMode' => true, 'useCompressedJs' => false, 'allowAutoUpdates' => true, 'environmentVariables' => array( 'siteUrl' => 'http://example.dev/', 'sitePath' => '/Applications/MAMP/htdocs/example/html/', ), ), 'staging.example.com' => array( 'environmentVariables' => array( 'siteUrl' => 'http://staging.example.com/', 'sitePath' => '/var/www/vhosts/staging.example.com/html/', ), ), );
And an example
return array( '*' => array( 'tablePrefix' => 'craft', ), 'example.dev' => array( 'server' => 'localhost', 'user' => 'root', 'password' => 'root', 'database' => 'example_dev', ), 'staging.example.com' => array( 'server' => 'localhost', 'user' => 'example_staging', 'password' => '********************', 'database' => 'example_staging', ), );
For more on Multi-Environment Configs see Multi-Environment Workflows on Straight Up Craft
Version control with Git
You can definitely use Git. I version everything except for user file uploads and
/craft/storage and it works great.
.gitignore file in the root of my project looks like (specific to my setup, YMMV)
.DS_Store # Node/Sass node_modules html/css/public/main.css.map # Config dandelion.yml sftp-config.json craft/config/db.php
Then in each directory I want to ignore the contents of (craft storage and user uploads) I put a
.gitignore file with...
# Ignore everything * # But not this file !.gitignore
And as Matt pointed out in his other comment on your question, do see the answers on the related question What's the best practice for upgrading a versioned Craft site?