Managing Your WordPress Site with Git and Composer Part 1 – Storing WordPress in Git

By Gilbert Pellegrom

Welcome to the first post in a series on Managing Your WordPress Site with Git and Composer. Together we’re going to look at how you can store WordPress in Git and how you can manage the parts of your WordPress site that shouldn’t be stored in your Git repository (e.g. themes and plugins) using Composer or Git Submodules.

At first we will simply store WordPress in Git as an introduction to using WordPress with Git, however later in the series we will cover the more complex, but arguably better approach to managing WordPress core itself using Composer or Git Submodules.

Why Git?

Version control systems like Git have become a huge help to developers in recent years, allowing collaboration on a level that previously just wasn’t possible. However it’s not always easy to get started using Git, especially if you’re trying to work with a big, established project like WordPress.

However the benefits of learning how to manage your WordPress site using Git are huge. Not only can you use awesome Git features like rolling back breaking changes and using branches for bug fixes and feature builds, but you can use amazing apps like GitHub to store your site and Dploy to deploy changes to your site with one click.

In this series we will approach most aspects from a beginner point of view, however I am going to assume you have a basic knowledge of Git and Composer and are comfortable using the command-line.

So, without further ado, let’s get going.

Storing WordPress in Git

To begin with we are going to store the WordPress core itself in Git. While it might make more sense to store WordPress separately using Composer or Git Submodules, that gets a bit more complex and involves changing the structure of your WordPress site, so we are going to leave that for a later post. For just now we’re going to focus on getting WordPress up and running and stored in Git.

Let’s start from scratch and create a new folder that will contain your WordPress install (or you could copy your existing WordPress site into a new folder if you wanted to). Open up Terminal, or whatever command-line client you use, and run:

mkdir wpgit && cd wpgit

In this tutorial we are going to use a relatively new project called WP-CLI. WP-CLI is a set of command-line tools that can help you manage your WordPress installations. For example you can update plugins, setup multisite installs and much more, without using a web browser. If you don’t have WP-CLI installed you can follow the installation instructions here.

So let’s download and install WordPress using WP-CLI. If you’ve copied over an existing WordPress site (or you don’t want to use WP-CLI) you can skip this bit.

wp core download
wp core config --dbname={YOUR DATABASE NAME} --dbuser={YOUR DATABASE USERNAME} --dbpass={YOUR DATABASE PASSWORD}
wp core install --url={YOUR DOMAIN NAME} --title={THE TITLE OF YOUR SITE} --admin_user={YOUR USER NAME} --admin_password={YOUR PASSWORD} --admin_email={YOUR EMAIL}


Next we need to make our WordPress install a Git repository. This is as easy as running:

git init

At the moment your new Git repo will be empty as we haven’t added any of our WordPress files to it. That’s because there are certain files and folders that we don’t want to add to the Git repo. This is because some folders might change often (like plugins and themes, we’ll be looking how to manage plugins and themes outside of Git in a future post) or your files might contain secure information (like database passwords).

Thankfully Git has a handy way of dealing with this problem. By creating a .gitignore file you can specify which files and folders you want to exclude from Git. Let’s do this by creating and editing the .gitignore file:

touch .gitignore
nano .gitignore

We can add rules to the .gitignore file to tell Git to ignore certain files and folders. Add the following rules to your .gitignore (lines starting with a # are comments):

# Ignore wp-config.php and .htaccess

# Ignore the wp-content directory


Note that you can, if you want to, add certain plugins and themes to the Git repo by including the path to the plugin or theme. For example:

# Don’t ignore plugins dir, but ignore everything inside

# Don’t ignore akismet plugin

Save the .gitignore file. As all “user managed” files (e.g. plugins, themes and uploads) are stored in the /wp-content folder, it’s easiest to just ignore the whole folder.

Now we’re ready to add the rest of our files to Git. We can add our files and commit them by doing:

git add .
git commit -m "Initial commit"

Great, you now have a WordPress site stored in a Git repository. There are a couple of things to remember when using this method:

  • It’s best practice not to store secure information (like database passwords) in a Git repo (hence why we ignore wp-config.php). This means you’ll have to deploy wp-config.php manually.
  • Everything in the /wp-content folder won’t be stored in Git (plugins/themes/uploads etc.) so if you’re deploying or migrating your site remember to manually migrate these files.
  • The database won’t be stored in Git, but don’t worry WP Migrate DB Pro has you covered 😉
  • If you update WordPress locally, you’ll need to commit the changes to your Git repository. Be aware that when using this method WordPress will disable automatic updates (because it will detect a .git folder). This is good as it means there is a one-way deployment of updates to your site (rather than your live site becoming out of sync with your Git repo) as long as you are aware keeping your site up-to-date is important for security reasons. But we’ll talk more about this in a later post.

That’s it for part 1. In the next post we’ll be looking at how to use Composer to manage your plugins and themes for your Git stored WordPress site.

About the Author

Gilbert Pellegrom

Gilbert loves to build software. From jQuery scripts to WordPress plugins to full blown SaaS apps, Gilbert has been creating elegant software his whole career. Probably most famous for creating the Nivo Slider.