How to Use Git for WordPress Development
You know Git, right? It’s one of the most popular version control systems in use today. Plus, it’s everywhere: Pre-installed by many hosting providers, used as the system that operates behind GitHub and Bitbucket, and now even built into easy-to-use WordPress plugins.
There’s been a lot of noise about Git in the WordPress community lately thanks to the development of Git-powered tools likes VersionPress, WP Pusher, Revisr, and Gitium. Like many other WordPress professionals, I’m a freelancer working primarily on an independent basis and I’ve never had to embrace version control. However, all of the Git noise as-of-late has led me to a realization: My Git skills need some attention.
Maybe, like me, you’re one of the many WordPress freelancers and developers who hasn’t yet fully embraced Git or another version control system. Maybe you’re part of small development team that has realized you need a better way to keep track of your collaborative projects. Or maybe you’re studying WordPress development and want to learn Git, because at this point version control expertise is a skill expected out of every professional developer.
I’ve researched how Git is being used by experienced WordPress developers and the tools available to make it easier to pair Git and WordPress. In this article, I’ll share what I’ve learned, and point out resources you can use to get started with Git for WordPress.
What is Git?
Git is a version control system. I’m sure you already knew that, but what does Git actually do?
Let’s look at a simple example of what Git can do to track the creation and editing of a document:
- The document is created.
- The document is saved.
- The document is edited.
- The changes are saved.
The first two steps only happen once. However, the last two steps typically happen many, many, many times.
What Git does is:
- Keep track of every time modifications are saved.
- Include a history of the state of the document before and after the modification.
- Provide you, the author or editor, with the opportunity to add a note explaining the nature of each change.
The result is that Git creates a complete history of every change made to the document from its inception, with the ability to roll the document back to any prior state.
Don’t Just Track a Document, Track a Project
Git isn’t limited to tracking a single document. As a matter of fact, Git tracks all of the activity in a specified folder including any subdirectories of that folder.
Most web applications, such as WordPress, are composed of multiple files that reside inside of a directory and multiple subdirectories. Git can be used to keep track over every change in one or all of these directories and subdirectories, and you can even tell Git to ignore certain files and directories as well.
Just like the single document history we mentioned, Git can be used to keep track of the history of an entire project that consists of a top-level directory, multiple subdirectories, and dozens of files.
And Don’t Just Track a Project, Collaborate
Git really begins to shine when it is used in a collaborative environment. Git is designed to maintain a central version of a project so that multiple contributors can work on project files simultaneously and merge all of their changes with the central version.
Git even notices conflicts between edits made by different team members and provides the opportunity to resolve those conflicts.
And Don’t Just Collaborate on a Project, Iterate
Let’s say you’re working on a project with a group of collaborators and you have an idea you want to try out without committing the entire project to going in that direction. Git makes this easy.
With Git you just create a new branch of the project, work out your idea, and then discard your branch if it doesn’t work out, merge it with the original project if it does, or save it as an entirely new project (referred to as forking – WordPress was born as a fork of b2/cafelog, an earlier blogging platform).
Change your mind about all those changes after a good night’s sleep? No problem. With Git’s version history, you can rollback the changes and undo the modifications.
Where Does Git Live?
Most Git workflows involve managing the flow of a project between three different locations:
- A local development environment: Git is used locally to track changes as you work on a local copy of a development project. You can optionally push a project from the local development environment to a Git host, or just keep it local if you’re the only one working on it.
- A Git host: GitHub and Bitbucket are two of the most popular Git hosts, but there are others. A Git host is a place where the master version of a project resides so that more than one developer can work on it at a time.
- A web server: With Git installed on the server, use SSH to clone the repository from the Git host you selected to the directory where the website will reside on the web server. More and more hosts offer Git installed even on low-cost shared servers.
How Do WordPress Developers Use Git?
What does a WordPress development workflow look like with Git thrown in the mix?
WordPress developer Josh Pollock detailed his workflow in our article No More Cowboy Coding: Improving Your WordPress Workflow.
Josh uses Git in three different places: A local development environment, a private Bitbucket Git repository, and a web server hosting the production version of the WordPress site.
Here’s how Josh’s system works:
- The official version of a site lives in the Bitbucket Git repository.
- SSH is used to initiate command-line Git requests on the web server to pull or push project files between Bitbucket and the server.
- SourceTree, a Git GUI, is used to initiate Git requests locally to pull or push project files between Bitbucket and the local development environment.
- Git, initiated over SSH on the web server and manipulated with SourceTree locally, is used to track changes and push/pull files, but an additional plugin, WP Migrate DB Pro, is used to make sure the database migrates back and forth.
This isn’t the only model for using Git and WordPress, but the three-part model – local, Git host, and web server – is pretty universal. The differences center on the software tools used to interact with Git and the choice of Git host.
WordPress-Git Red Flags
There are a few problems endemic to using Git and WordPress together:
- How do you handle wp-config.php since it will be different on the local and production servers and you don’t want it exposed in a public repository?
- How do you deal with media files? Does it make sense to copy them all from the production server to your local development environment on a periodic basis?
- Does it make sense to include the entire WordPress core and third-party themes and plugins in your Git repository, or should you limit Git’s tracking to custom plugins and custom theme modifications?
- How do you keep the database updated between the local and production servers?
You can find out how Josh recommends dealing with those issues by reading the full post to get all of the details.
Meanwhile, full-stack developer Steve Grunwell explains his workflow at the website Keeping WordPress Under [Version] Control with Git.
Steve’s workflow looks an awful lot like Josh’s. However, it’s worth taking a look at because Steve’s explanation includes two valuable, time-saving bits of information.
- He provides text for a recommended .gitignore file.
- He provides an
IfModuleyou can drop into your local site’s .htaccess file.
These files work together to do two things: limit the directories and files included in the Git repository, and tell the local version of the site to load media files from the web server. This eliminates the question of whether or not you should copy media to your local environment and excludes certain directories and files (such as wp-config.php) from your Git repository.
Steve’s workflow description is not entirely beginner-friendly. It leaves several assumed steps out–such as the fact that the
IfModule should be dropped into the local .htaccess file. However, once you have some Git experience, it will all make a lot more sense.
Now that we have a pretty idea for how WordPress developers use Git to manage WordPress projects, let’s take a look at some of the Git tools used by WordPress developers.
Git was initially designed to run from the command line, and many developers still use the command line to work with Git. However, you can get away from using the command line to work with Git locally if you want to. If both Git and the command line are foreign to you, using a Git GUI can make the process of learning Git easier by allowing you skip the command line for now.
SourceTree is a very popular and free Git client that integrates with both Bitbucket and GitHub. GitHub Desktop is another popular option, but one that only works with GitHub. There are several other Git GUIs you can consider. Just make sure that the client you settle on works with the Git host you plan on using.
A Git host is a home for your Git repositories on the web. You don’t have to use a Git host. It’s entirely possible to push changes from your local environment straight to your web server with SSH or just to use Git for local version control.
Using a Git host means you have a complete backup of your website files in a place where it’s easy to push them to a web server over SSH at a moment’s notice. In addition, if you want to collaborate with another developer, using a Git host to manage and merge changes to the project will make the process a lot easier.
Obviously, GitHub is the most well-known Git host. However, they are far from the only option and really only a great option if you want your repositories to be public or don’t mind paying a few dollars ever month for private repositories.
Bitbucket is another very popular option, especially for developers who prefer to use private repositories. Another well-known option is GitLab and there are several additional Git hosts if you aren’t sold on GitHub, Bitbucket, or GitLab.
Git Tools for WordPress
You don’t have to use any WordPress-specific tools to incorporate Git into your workflow. However, since you’re getting started with Git, there are several WordPress tools you will definitely want to know about.
VersionPress creates a Git repository on your site’s web server and tracks every single change you make. It also adds a dashboard to the WordPress admin where there is a literal undo button available for every change.
You can use VersionPress to undo any change you make, including rolling back the WordPress core after an upgrade, undoing a plugin update, or uninstalling a theme. Also, if you’re comfortable with WP-CLI, you can use VersionPress to create staging sites which you can then merge with the production site once your staging site is ready for public consumption. Our previous post about VersionPress will help you with Setting Up VersionPress for Git-Powered WordPress Version Control.
It’s worth mentioning that VersionPress is still an early release plugin. What this means is that it isn’t a finished product quite yet, and you should exercise caution if you decide to use the plugin.
Revisr is very similar to VersionPress with one major exception. VersionPress tracks every change you make to your site automatically and Revisr tracks changes when you commit them to the Git repository manually. Revisr is a production-ready product available from the WordPress plugin directory and works well if you think of it as a backup plugin that uses a Git repository to store the site backup.
Revisr does offer the ability to track database changes, but the plugin cannot merge two versions of a database and therefore cannot be used to create and merge staging sites. However, unlike VersionPress, Revisr is a finished product and you should come across fewer plugin and theme conflicts with Revisr than with VersionPress in it’s current early release state.
If you think about it, the WordPress core is already under version control. In addition, the core includes versioning of all page and post changes. So what’s left? Plugins and themes. This is where Gitium comes in.
Gitium, from Presslabs, brings Git-powered versioning of any changes to plugins and themes right to the WordPress admin dashboard. All plugin installations, code changes, and updates are automatically tracked and versioned in a repository hosted by the Git host of your choice, such as Bitbucket or Github.
WordPress GitHub Sync keeps a running history of edits to your content in a GitHub repository. This is especially useful if you have content that is modified by many different users since each change will be attributed to the logged-in users who makes the edits. You can even use the plugin to accept reader submissions made through GitHub.
If you want to use WordPress GitHub Sync you will also need to install WP-Markdown which will convert your posts from HTML to markdown for improved rendering and simple syntax when viewed on GitHub.
WP Pusher connects your WordPress website to a WordPress plugin or theme hosted in a GitHub repository. This makes it much easier to install and update WordPress plugins and themes that are hosted at GitHub.
Think of WP Pusher as a bridge for plugins and themes that connects your WordPress site to GitHub. Without WP Pusher, you have to use SSH or FTP to use plugins or themes hosted at GitHub. With WP Pusher you can easily install or update GitHub-hosted plugins and themes right from the WordPress dashboard. This means WP Pusher is also a useful development tool when developing your own custom plugins and themes and using GitHub for versioning.
WP Pusher is free if the plugins and themes you use are hosted in public GitHub repositories. However, working with private repositories will trigger the requirement to purchase a license.
Get Started Learning Git
If you’ve made it this far, you must be seriously committed to learning Git and using it to improve your WordPress development workflow. I’ve tracked down some high-quality Git education that will take you from Git beginner to competent user in less than eight hours.
- Try Git is a quick interactive introduction to Git from Code School and GitHub. Burn through it in 15 minutes to get a feel for how Git works and to learn some of the basic commands. Time to complete: 15 minutes.
- The Git Basics video series provides a high-level overview of version control and Git. Time to complete: 30 minutes or less.
- Learn Git is a free course from Codecademy that will get you comfortable working with basic Git commands such as
log. Time to complete: 2 hours.
- You don’t have to use command line to work with Git locally, but most developers do. Also, if you’re going to use Git on a server over SSH, you will have to use the command line. For this reason, I’ve included Learn Command Line on this list. It’s another free course from Codecademy that will teach you how to use command line to navigate between diretories and files. Time to complete: 3 hours.
- Git for WordPress Developers includes 35 screencasts put together by the developer behind WP Pusher. Screencasts cover topics like ignoring files, advanced Git commands, and deployment strategies. Time to complete: 2 hours or less.
Make it through those free resources and you’ll be equipped to use Git competently while you continue to learn as you incorporate Git into your workflow. However, if you really want to develop professional-grade version control chops, then take a look at Pro Git by Scott Chacon and Ben Straub. This 500+ page book is available for free in a varity of ebook formats, or you can get a dead tree version from Amazon.
The Git website also provides a list of many additional useful resources for learning Git in case you don’t see exactly what you’re looking for in the list above.
WordPress is growing up. It’s transitioning from being a blogging platform to being the beating heart of a powerful CMS and web application platform. Integrating version control into mainstream WordPress development workflows is natural step in the maturation of the platform.
If you’re serious about staying on the cutting edge of WordPress development – and I know you are, because you’re reading this blog – you have to add Git competency to your WordPress developer toolbox. Making it through this guide to Git for WordPress is a great first step.
Next, schedule the time to work through one or more of the recommended free resources and start integrating Git into your WordPress development workflow.Tags: