Using a version control system is helpful for multiple developer environments, as well as individual tracking and archiving of changes. Here’s how I set mine up using Git.
Note: Setting up a cloned repository between dev and live servers is useful for theme and static file tracking. You wouldn’t want to setup Git on an entire WordPress directory, however, because tracking uploaded files from the live site “Uploads” folder would cause tracking problems. For WordPress, specifically, I like to setup Git inside my custom theme directory, where files will not likely be changed on the live website.
Initialize Git on your remote server
- SSH into your remote server:
$ ssh <username>@<host>
- CD to the theme directory:
$ cd www/wp-content/themes/<custom_theme_name>
- Initialize Git:
$ git init
- Add the current files (if any):
$ git add .
- Commit the addition:
$ git commit -m "Initial Commit"
Clone a bare repository outside the web root
$ git clone --bare /home/<username>/www/wp-content/themes/>custom_theme_name> /home/<username>/proj.git
Clone the bare repository to your local server
- CD to your local server directory. Mine is in MAMP htdocs:
$ cd /Applications/MAMP/htdocs/
- Create a dev directory by cloning the remote repository:
$ git clone ssh://<username>@<host>/home/<username>/proj.git <custom_theme_name>
This will create a local copy of your remote server files. Now you can make changes to these files and keep track of them using Git.
Add, edit, save your local files.
- Create a dev branch:
$ git branch dev
- Checkout the dev branch:
$ git checkout dev
- Make changes, edits, save files.
- Add them to Git.:
$ git add .
- Commit the changes:
$ git commit -m "Made changes"
- Checkout the master branch:
$ git checkout master
- Pull changes from the origin, to see if any changes have been made (if working in a multiple developer environment):
$ git pull origin
- Resolve any conflicts in code.
- Merge the dev and master branch:
$ git merge dev
- Push the changes to the remote repository:
$ git push origin
Pull changes to live website
- SSH into remote server
- CD to theme directory:
$ cd www/wp-content/themes/<custom_theme_name>
- Pull new files and edits from the repository:
$ git pull /home/<username>/proj.git
And, voila! You have a working repository environment using Git to track changes to your custom WordPress theme, or other static file directory.
Any questions?