WordPress lead developer, Mark Jaquith, recently gave a talk at WordCamp San Francisco on Scaling, Servers, and Deploys that including some great advice on professional WordPress development using Git. Git is a free & open source, distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
The session from WordCamp San Francisco:
The purpose of this guide is to show you how you can add Git to your WordPress development workflow and will include the basics of securely installing Git on a remote server, using your local machine for development and deploying your changes to a live production server using SSH.
This article assumes:
- You have SSH access to a remote server running Debian or Ubuntu (The shell commands will be slightly different for other distros)
- Your using a Mac with a local development environment already set up.
- Your comfortable using the command line and able to access your remote server using SSH keys.
Step 1: Installing Git on the remote server
Install Git:
apt-get install git-core
Adding the git user. (You will be asked to enter a password for the git user. We won’t be using it but remember what it is just in case.)
adduser git
Setup SSH keys for the new git user. If you still have access to the public key you use to access your server you can add it to /home/git/.ssh/authorized_keys. If not you will need to generate a new public/private key pair and add the public key to your server. See my previous article WordPress Performance Server – Debian “squeeze” with Nginx, APC and PHP from the Dotdeb repos for more info on setting up SSH keys on your server. You can also use the ssh-keygen tutorial on GitHub.
su git cd /home/git mkdir .ssh chmod 700 .ssh cd .ssh touch authorized_keys cat id_dsa.pub >> authorized_keys exit
If you need to switch back to your normal user to add your public key make sure you chown the /home/git/.ssh back to the git user by running:
chown git:git /home/git/ -R
Now lets test our SSH connection using the git username on the remote server. If you use lots of different SSH keys for different servers it helps if you add instructions to your config file to identify the right key to use when logging in.
On your local machine create if needed and open the file ~/.ssh/config
Host remote_server_domain.com Hostname remote_server_domain.com IdentityFile ~/.ssh/id_dsa //Your private key that matches the public key we installed on the server User git
Now lets test our connection to the remote server
ssh git@remote_server_domain.com
If your able to connect let’s go ahead and create and initialize our remote Git repository. We’re calling this repo “project1″.
cd /home/git mkdir project1 cd project1 git --bare init exit
You just created your first remote Git repository!
For security we are going to restrict shell access for the git user. This will allow you to share development with other users by giving them SSH access to the repo without giving them shell access to your server.
SSH back into your server with the user with su privileges.
nano /etc/passwd
When you open the file find the line for the new git user we just added and change the end of the line from /bin/bash to /usr/bin/git-shell. (Do not change the user Id or group ID). It should look like this when your done:
git:x:1001:1001:Git,,,:/home/git:/usr/bin/git-shell
Test the new config by exiting and trying again to SSH into the server with the git user:
ssh git@remote_server_domain.com fatal: What do you think I am? A shell? connection to [remote_server_domain.com] closed.
Now we have a secure remote repository.
Step 2: Installing Git and setting up your local machine
Download and install the latest version for Mac (For Lion or Snow Leopard choose: Git Installer 1.7.6 – OS X – Snow Leopard – x86_64)
Making your first commit.
Now that we have our remote repo setup and Git installed on our local machine let’s make our first commit.
mkdir project1 cd project1 git init touch readme.txt git add . git commit -m "Initial commit - added readme.txt"
The first time we use our new repo we need to tell Git where to find it.
git remote add origin ssh://git@remote_server_domain.com/home/git/project1 git push origin master
Now we are going to delete the project1 directory we created on our local machine and check out the repo so we can start doing some WordPress development. I use the ~/sites directory for my local development so lets check the new project out into that directory. If you haven’t set up your local environment using hostname aliases in the ~/sites directory check out this tutorial on WPCandy: How to improve local WordPress development on a Mac
rm -rf project1 mkdir ~/sites/wp.dev cd wp.dev git clone ssh://git@remote_server_domain.com/home/git/project1
We now have checked out the latest version of our repo (currently only containing the readme.txt file) and are ready to start working but first we need to decide our version control strategy.
Next Page – Step 3: Setting up your repo for WordPress theme and plugin development














