Git is Version Control software that allows you to track changes made to any type of development code and revert those changes with the click of a button. Consider the following case by NICK SCHÄFERHOFF at Torque Magazine:
Does the following sound familiar? You want to make a change to your WordPress site. So you fire up the FTP connection, locate your desired file and download it, make the necessary changes and upload it back to the server. After that, you refresh your browser page to see the changes in effect.
If that is your workflow, don’t worry. You are not alone. Everyone starts out like that.
It’s only later that people professionalize with things like local development and trying out design changes via Firebug before committing changes to style.css. It’s a natural process and you shouldn’t be ashamed to progress along it in your WordPress journey.
However, that also doesn’t mean that you shouldn’t strive for improving the way you work. Therefore, in your quest for an ever better development workflow, we want to add another weapon to your arsenal: It’s called version control.
Have you ever made changes to your site that didn’t quite work and then couldn’t remember what you did and painstakingly had to figure it all out again?
Or, have you wanted to go back to an older version of your website or WordPress theme but didn’t have any backup?
Version control is the safety net for cases like that. It is a system that tracks changes to one or several files and stores information on who changed what at which point in time.
That way it allows you to revert your website or project back to previous versions (hence the name) and keep an overview over what has happened to your files through time.
If you’re developing locally, the best way to use Git in your project is to set up a repository for your WordPress theme code. You can do this with GitHub or BitBucket, although BitBucket allows you to have unlimited private repositories for free, so it’s the one I’m using in this example. You can then work on your WordPress theme locally, and when you’re done working on a feature, you can commit those changes to your remote BitBucket repository for safekeeping and collaboration.
I’m also using a free GUI tool for git called SourceTree. It makes it easy to keep track of your repositories in GitHub or BitBucket, and commit any changes you’ve made locally to those remote repositories.
So to get started:
- Sign up for a BitBucket account.
- Download and install SourceTree.
- Create a new repository in BitBucket:
- Clone your repository locally in SourceTree:
- Clone the repository into a new directory in your wp-content/themes folder in your local WordPress development files. If you’re using the Genesis Sample theme, name your folder ‘genesis-sample’:
- SourceTree will now open that repository on your local machine. You’ll see that the there are currently no files in the repository:
- Copy your WordPress starter theme files into the new folder created by SourceTree. In our case, these would be the Genesis Sample files:
- Go back to the SourceTree window. You’ll now see that SourceTree has noticed that the contents of your repository folder have changed, and suggests committing those to the master repository:
- Check the “Unstaged Files” checkbox to select all of the new files to be committed to the master repository:
- Click the “Commit” button in the upper left-hand corner of the SourceTree app, add a Commit comment, and select the “Push changes immediately to origin/master” checkbox. Then click the blue “Commit” button to finalize the commit.
- Now you’re ready to start working on your project! First off, let’s make some changes to style.css:
- If you go back to SourceTree, you’ll see that it has tracked your changes, and suggests committing those changes to the master repository. The line highlighted red is the original code, the line highlighted green shows the recent changes:
- Commit your recent changes to the master repository in the same way as the Initial Commit. Give each commit a unique description to keep track of your changes:
- If you need to reverse a commit, just right-click on the commit in the History window and select “Reverse commit.” Then click the “Push” button in the toolbar to apply those changes to the master repository.
This is a very basic introduction to using version control with WordPress. There are numerous in-depth tutorials out there on using the more complex features of Git available. A good place to start is:
Using Git in a Team Environment
Git is also beneficial because it allows you to work on projects with a team, without running the risk of overwriting each other’s file changes. Git tracks changes to a file line-by-line, allowing you and your team members to review any conflicts before committing those changes to the shared repository. Being comfortable using Git is a requirement for most team development environments today.