Tutorials WordPress

How to Use Git with WordPress

Written by ellice


You’ve probably heard of Git by now, but you might not be familiar with the system beyond its name. Perhaps you’re a beginner just getting started with development, or maybe you’re curious about how you can apply Git with WordPress but don’t know where to begin. Either way, the task at hand may seem a little overwhelming at first.

Fortunately, these days it’s easier than ever to implement Git on your WordPress site. Behind all the jargon, Git is simply a version control system that enables you to keep track of all the changes that are made to a file. With Git, you can maintain a complete history of the file and easily revert to any previous version. As you’ll soon see, there are many applications for this simple technology.

In this guide, we’ll provide you with a basic introduction to Git, then help you find the resources and tools you’ll need to get started with it. Finally, we’ll show you how you can use Git to manage a child theme. That’s a lot to cover, so let’s get started!

An Introduction to Git

At its core, Git is a version control system. When it is installed on your server, it will keep a complete history of every file in whatever project it’s connected to. This means that when a change is made to a file, Git will save the old version and record exactly what alterations were made.

This process is repeated every time the file is modified, enabling you to go back to each point in a project’s history and easily retrieve a specific version. In a nutshell, this gives you an unlimited number of ‘undo’ actions, so you never lose anything. Plus, it lets you (and others) see how the project has developed over time.

Two features that set Git apart from most version control tools are branching and merging. Through these processes, you can split a project into several ‘branches.’ These branches enable you to work on specific aspects of the project separately, without impacting the entire thing.

For example, imagine that you’re working on a live website and you have an idea that you want to test. You can create a branch of your main Git project, which you can make changes to and test them on. If your idea turned out great and you want to implement it on your site, you can ‘merge’ that branch (and its changes) into your main project. Otherwise, you can simply discard that branch. Even if you merge your branch, but regret doing so, Git enables you to easily revert to the previous version of the project.

Branches also enable you to work collaboratively with other developers. All developers can create their own branches, and commit them back to the main project when they’re done working. This is actually how WordPress itself is developed, with contributors creating branches, working independently on their ideas, and then submitting proposed changes to the core platform.

If you want to know more about how to use Git, there are several resources available online, such as Learn Git, and Try Git. We also recommend that you learn how to use the command line, if you want to get the most out of developing with Git. The most common way of using Git is through Secure Shell (SSH), which uses a command line interface.

How Git Works (In a Nutshell)

How you use Git depends largely on your workflow and the specific project at hand. Git is a versatile system that can be implemented in different ways, to match varying goals. For the most part, however, you’ll want to use it for customization projects, such as developing plugins and themes. We recommend that you create a repository for each project, so you can easily keep track of your work (especially if you’re part of a larger team).

To start understanding how Git actually works, let’s take a look at the three environments where it usually ‘lives’:

  • Local environment. This is your actual computer, where you can use Git to track changes when you work on your local copy of a project. If you’re the sole developer, you can keep your changes local, but if you’re working on a collaborative project you can push your changes to the Git host.
  • Web server. This is the server where your website is hosted, and where the site that you’re developing and pushing changes to will be located.
  • Git host. Finally, this is an external host that contains the master version of the site and is most relevant if you are not the only developer on a project. The Git host enables all developers to pull content from the same project and implement their changes. Two of the most popular Git hosts are GitHub and BitBucket.

Keeping this structure in mind, you can create a workflow depending on what you require in your project. Let’s look at some basic examples of how you might use Git:

  • Local only. You can simply use Git on your computer to keep track of your own work, and then use an SFTP solution to update your server with the changes.
  • Local and web server. You can create a Git repository on your computer and one on your web server. When you have made changes locally, you can push them to your server (so they’ll appear on your live site).
  • Local, Git host, and web server. If you are working with other developers, you can use a Git host to contain a repository for your project. Everybody can push their changes to the Git host, and you can push updates to the web server whenever you choose.

These are all common applications, and you’ll want to pick the workflow that best suits you and your project. There are also plenty of workflow examples available, to help you get a feel for the possibilities.

Getting Started With Git

When you’ve done your research and you’re ready to get started, you will need to install Git in the environments where you intend to use it. Doing this with your local environment is easiest since you can download and install Git using either a command or an installer.

The next step is setting up a Git repository on an external server. If your site is hosted on DreamHost, it will already have Git pre-installed, so this is a simple task. All you need to do is create a user identity and initialize the repository, using some basic commands.

Once you have Git set up, you’ll need to decide how to use it alongside WordPress. There’s no single method that’s best for everyone. As we’ve already mentioned, Git is incredibly versatile, so how you want to use it depends largely on your project requirements and personal preference.

We mentioned earlier that many developers use the command line to work with Git. This was its original application, but there are now a number of graphical user interface (GUI) tools you can choose from. If you’re unfamiliar with using commands, having a graphical interface might be preferable.

Let’s look at some of the most popular Git GUI tools. All of these come included with Git, so you don’t need to download each one separately:

  • SourceTree — This free client features a simple interface and integration with both BitBucket and GitHub.
  • Tower — This is one of the most powerful clients, with a great user interface. Tower offers a 30-day free trial, after which you need to purchase a license (which starts at $79).
  • GitHub Desktop — This client is free and open source, so you can customize it yourself. Bear in mind that it only works with GitHub, however.
  • TortoiseGit — Finally, this free, open-source client is a Windows shell interface based on TortoiseSVN.

Before we continue, we should also look a little closer at the two most popular Git hosts. We’ve mentioned them in passing already, but if you use Git regularly, you will need to become familiar with both sooner rather than later.

The most commonly used host is GitHub. This is a great option for code that you want to share publicly, since it offers free public hosting. However, if you want to set up a private repository, you will need to purchase a plan starting at $7 per month for a single user.

If that puts you off, you might instead want to look at BitBucket, which is a smart alternative for private repositories. BitBucket is free for smaller teams, and its prices scale depending on use. For teams larger than five people, the price starts at $2 per month per user.

3 Plugins For Using Git With WordPress

When you have implemented Git in your chosen environments, you can start using it to develop for WordPress. There are several plugins available that let you do just that. What follows are three of the best options for beginners and advanced users alike.

1. VersionPress

VersionPress is one of the most versatile and comprehensive Git plugins for WordPress. It sets up a repository on your server and enables you to revert every change easily. This even includes rolling back the WordPress core after updates. VersionPress also enables you to easily branch and merge sites, even including the database.

Key Features:

  • Offers a simple and intuitive admin interface that doesn’t require you to know Git or SSH.
  • Is compatible with all major Git hosts.
  • Supports all Git workflows.

Pricing: The VersionPress plugin is entirely free, with no premium editions.

2. WP Pusher

WP Pusher is a plugin that connects your WordPress site to any Git host, enabling you to transfer plugins and themes between the two. As a developer, this enables you to easily install plugins and themes onto a site, without needing SFTP or SSH.

Key Features:

  • Is compatible with multisite setups.
  • Doesn’t require Git to be installed on the server.
  • Easily deploys themes from all major Git hosts.

Pricing: The free version of WP Pusher can be used with public repositories. It also offers multiple paid plans, starting at $99 per month.

3. WordPress GitHub Sync

As the name suggests, WordPress GitHub Sync enables you to sync your WordPress site with any Git host. It offers standard Git functionality, recording all changes to your site along with the users who made them. However, it also enables external users to submit proposed changes through GitHub. What’s more, you can sync to a Jekyll site if you want to, which enables you to maintain it using the WordPress interface.

Key Features:

  • Enables external users to submit through GitHub.
  • Tracks all edits, along with the names of the users who made them.
  • Is compatible with GitHub and with Jekyll sites.

Pricing: WordPress GitHub Sync is 100% free, with no premium editions.

How to Use Git With a DreamPress Child Theme

By now you’ve become familiar with Git as a concept, and have an understanding of how it can be used alongside WordPress. It’s time to put that theory into practice and illustrate one of Git’s applications.To do that, we’re going to show you how to use Git with a DreamPress child theme.

This example will show you step-by-step how to configure Git, and then use it to version control a child theme. This will get a little technical by necessity, and require you to use the command line and SSH.

To begin, if you have not already set up a shell user, you’ll need to do that first. You can get this done in the DreamHost panel, after which you’ll need to save your shell username, password, and server information. The following is an example of how these might look:

Shell username: wp_ab1cde
Server: wp_123456.dreamhostps.com

Next, you’ll need to set up an SSH key. This will eliminate the need to enter your password every time you log in via SSH. How you do this depends on your operating system, but you can follow our guide on the process for more information.

When you have your SSH key in hand, you will be able to use it to create a connection. The command for that is simply ssh followed by your key. So if you used the same credentials as above, it might look like this:

ssh wp_ab1cde@wp_123456.dreamhostps.com

You now need to select a terminal application to use. Mac and Linux both include terminal applications, but if you’re a Windows user you’ll need to download one. We recommend that you use PuTTy, although you will need to configure it somewhat.

Then, open the terminal you want to use. You can now enter the ssh command listed above (with your SSH key) to connect to your server. If this is the first time you’re doing this, you will be asked to accept that the server is a valid host. After that, your computer will know to trust this host when you connect to it in the future.

It’s now time to actually set up your Git repository, which you’ll do by first creating a new directory. You can place it wherever you want using the following command, as long as you remember where you created it:

mkdir -p git/projectname

In this case, we’re creating two new directories called git and projectname respectively. The following command will take us to one of the new directories:

cd git/projectname

You can then initialize the repository with this command:

git init --bare

Your new repository is now ready! Remember that you should always create a separate repository for each project.

The next step is to create a file that will copy updates from your repository to your child theme directory. In this case, we’re going to use nano to create the file, as it’s a solid application for beginners. Make sure you’re still in your repository directory and then run this command:

nano hooks/post-receive

You can now add the following code to this file:

#!/bin/sh GIT_WORK_TREE=/home/shell-username/example.com/wp-content/themes/child-theme-directory git checkout -f

This should all be on a single line, and you will need to make some changes to it:

  • shell-username should be your actual shell user.
  • example.com should be your website.
  • child-theme-directory should be the name of your child theme directory.

When you’ve updated the file, save your changes and close it to return to the shell. You’ll now need to give the file execute rights, which you can do with this command:

chmod +x hooks/post-receive

At this point, your Git repository is up and running, and connected to the child theme directory. If you haven’t already installed Git on your local machine, you should do so now. You can use the information from earlier in this article and the official documentation for assistance.

It’s now time to create your local Git repository. First, use this command, replacing the path with that of your child theme’s directory:

cd $HOME/example.com/wp-content/themes/child-theme-directory

Now, run the following command to initialize the repository:

git init

You are now free to develop your child theme on your local machine. We won’t cover the specifics of how to do that in this article, but you can refer to our comprehensive guide and other previous pieces for more help. When you’re done, you should save your changes while still in the child theme directory:

git add .
git commit -m ‘commit message’

The ‘commit message’ string can be replaced with any text you want to save along with the commit. A good idea for a first commit is to use ‘initial commit’ as the message.

You now need to set up the connection to your remote server. Make sure you’re still in your child theme directory and run the following command:

git remote add myserver ssh://username@server.domain.com/ ~/git/project

You can replace myserver with any name you want to give to the server. The username and address should also be changed to match your real details. Your local and server repositories will now be connected, which means that you can push changes to your server using this command:

git push myserver +master:refs/heads/master

You’ll only need to enter this command the first time you want to push a change. Afterward, you should use the following command instead:

git push myserver

With that, you’re done! You have successfully created local and server repositories, connected the two, and pushed your local data to your server. As we mentioned at the start, this is only one example of how you can use Git with WordPress. Feel free to experiment further, and lean on all the resources we’ve referenced for additional support.

Git Going

Dipping your toes into the waters of Git can be a little daunting. However, once you understand the underlying concepts of the system and how it can be implemented on a WordPress site, you’ll be ready to put all that knowledge into action.

About the author

ellice

Ellice comes to us from House DreamHost, the first of her name, Gatekeeper of All Things Content, Protector of the Brand and Ruler of Social Realms.

tl;dr - Ellice is the Content Marketing Manager at DreamHost and oversees all social media and content efforts.

Leave a Comment