WordPress Version Control with Git

Share this article

WordPress Version Control with Git

This article is part of a series created in partnership with SiteGround. Thank you for supporting the partners who make SitePoint possible.

Version control is an integral part of the web development workflow, and it is no less necessary with WordPress sites. However, getting a WordPress site set up with version control, or more specifically, with Git, can be challenging in several ways. Knowing what to commit to your Git repositories and what to ignore can be challenging. Syncing database changes can be similarly so. And WordPress, with its ease of updates and additions straight to live sites, makes it incredibly easy to update the live site directly, disrupting the version control process.

Below are a few options for using Git with WordPress, some information about VersionPress, the well known Git plugin, as well as a brief mention of hosting-based Git implementations.

Using Git with WordPress

Using Git with WordPress can be a challenge. Here are a few tips that can get you going the right direction (note that these tips are assuming you already have a basic familiarity with Git):

Regarding your database connection, you should do one of two things: Either use the same exact database name and credentials in development, staging, and production environments, so that there is no difference in your wp-config.php database connection info, or else .gitignore your wp-config.php entirely so that it doesn’t get overwritten with the info from your local development environment.

Speaking of things to ignore, you should also probably .gitignore your uploads directory. It’s unnecessary to be syncing uploads, and uploads are the one thing that may be added to the production file system only, so no need to cause unnecessary sync problems!

Disabling certain abilities within WordPress can be useful as well.

  • Disable automatic updates by adding define( 'AUTOMATIC_UPDATER_DISABLED', true ); to wp-config.php. This will stop the automatic updates from occurring altogether on the productions site.
  • Disable the admin panel’s file editor by adding define( 'DISALLOW_FILE_EDIT', true ); to prevent it from being used to modify theme code and other files on the production site.
  • Or, stop themes, plugins, etc from being modified or added at all (rendering the previous wp-config.php suggestions unnecessary) on production with define( 'DISALLOW_FILE_MODS', true );. This ensures that all updates are first done in a development or staging environment, and then pushed manually to the live site.
  • Note that usage of any of these restrictions should be coupled with creating a process to ensure that updates are regularly performed. Automatic updates exist for a reason, and if you don’t ensure your sites are up to date, you’re making them less vulnerable from one type of risk and more to another.

Backups created manually or by plugins should be stored somewhere outside of the Git repository (preferably to an offsite or cloud backup).

Using VersionPress

VersionPress is an alternative to traditional Git repository usage with WordPress websites. Installing VersionPress is as easy as any other plugin! As part of the installation process, VersionPress will check the prerequisites that it requires on the host system, and warn you or stop the installation if they are not present.

Once you’ve got it installed, you’re good to go! VersionPress tracks every change to the site – added, modified, and deleted posts or pages, plugin changes, etc. You can see a list of tracked events, and you can click “Undo” next to any single event to undo that specific past event, or you can click “Roll Back” to roll the entire site back to the state that it was in at the time of that event. Pretty cool!

On top of all of that, power users can use their normal Git client to manage the VersionPress install, since every action and command that VersionPress performs is powered directly by Git.

Host Provided Git Services

Some hosts provide their own version control services, allowing WordPress admins to benefit from the use of Git repositories to control their site, without having to set up a repository somewhere and maintain it. SiteGround , for one, uses a Git powered cPanel plugin to offer version control services from right within your cPanel, making it very easy to use.

No matter what you choose, version control is still very achievable for WordPress admins, and it’s the best way to keep your site updated, safe, and easy to manage!

Frequently Asked Questions about WordPress Version Control with Git

What is the importance of using version control in WordPress?

Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. In WordPress, it allows developers to track changes, revert to previous versions, and collaborate with other developers without the fear of overwriting or losing the original code. It provides a safety net and makes the development process more efficient and less stressful.

How does Git work with WordPress for version control?

Git is a distributed version control system that tracks changes in source code during software development. When used with WordPress, it allows developers to keep track of all changes made to the codebase. Git creates a repository of files as they’re changed, creating a history of changes that can be reviewed and even reverted if necessary. This makes it easier to collaborate with other developers and maintain the integrity of your project.

Can I use Git for version control if I’m a solo developer?

Absolutely. Even if you’re working alone, Git can be incredibly beneficial. It allows you to keep a history of your changes, making it easy to revert back if something goes wrong. It also makes it easier to manage updates and changes, especially if you’re working on a large project.

How can I start using Git for version control in WordPress?

To start using Git for version control in WordPress, you first need to install Git on your local machine. Once installed, you can initialize a new Git repository in your WordPress directory. From there, you can start tracking changes, committing changes to the repository, and pushing your changes to a remote repository.

What are the best practices for using Git with WordPress?

Some best practices for using Git with WordPress include committing often and writing clear commit messages. This makes it easier to track changes and understand the history of your project. It’s also recommended to ignore certain files in WordPress that don’t need to be in version control, such as the wp-config.php file or the /uploads directory.

Can I use Git for version control on a live WordPress site?

Yes, but it’s not recommended. Using Git on a live site can lead to downtime and other issues. It’s best to use Git in a local or staging environment and then deploy your changes to the live site.

How can I revert to a previous version of my WordPress site using Git?

To revert to a previous version of your WordPress site using Git, you can use the ‘git checkout’ command followed by the commit hash. This will change your files back to the state they were in at that commit.

How does Git handle conflicts in WordPress?

Git handles conflicts by marking the areas in your code where conflicts occur. You then need to manually resolve these conflicts by choosing which version of the code to keep. Once resolved, you can commit the resolved code to the repository.

Can I use Git with other version control systems for WordPress?

Yes, Git can be used alongside other version control systems like Subversion or Mercurial. However, it’s important to note that each system has its own strengths and weaknesses, and using multiple systems can sometimes lead to confusion or conflicts.

What are some common challenges when using Git for version control in WordPress and how can I overcome them?

Some common challenges when using Git for version control in WordPress include dealing with merge conflicts, managing large repositories, and understanding Git’s complex command line interface. These challenges can be overcome by learning more about Git’s commands, using a Git GUI client, and following best practices for version control.

Jeff SmithJeff Smith
View Author

Jeff works for a startup as a technical writer, does contract writing and web development, and loves tinkering with new projects and ideas. In addition to being glued to a computer for a good part of his day, Jeff is also a husband, father, tech nerd, book nerd, and gamer.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week
Loading form