21

I am trying to find a solution to a very difficult task: properly version controlling WordPress, specifically when working with automatic updates.

WordPress allows a lot of simplicity by allowing users to update WordPress core files, themes and plugins by simply clicking a button. But what happens when you have that website under version control with something like Git? As soon as we click that "Update Now" button, our Git repo will be out of sync and therefore defeats the purpose of creating a Git repo in the first place.

I have been looking into ways to get around this issue and all I was able to find was different ways to structure the Wordpress installation by breaking up components into Git submodules. One of the most popular example is the WordPress-Skeleton template.

Although this works to version control each module / component of WordPress, it still does not allow the user to be able to use the automatic updates button from within WordPress, as that will update the files in production but not commit those changes into your Git repo.

In an ideal world, we should be able to version control all the files in one repository and then when we click the "Update Now" button, it should update our Git repo with the modifications automatically. Does anyone know how this can be accomplished?

One of the things I was thinking was to create a plugin that would listen for update events and automatically commit changes as soon as that hook is triggered. Not sure if this is the best approach.

Please let me know if anyone has a better way I can accomplish this.

Agop
  • 1,907
  • 17
  • 23
Farhan Ahmad
  • 5,148
  • 6
  • 40
  • 69
  • 1
    I firstly would keep WordPress core out of git - and any third party plugins and themes as well. Make use of composer and bring everything in that way. You still have the issue if someone updates - there is no easy way to do this correctly to be honest. But composer will make it easy for you to keep a repository matched. The only thing I save in git is my own custom theme and wp-config.php etc and anything that has been edited. https://roots.io/using-composer-with-wordpress/ is a good starting point. You shouldn't need to store any files which are already under version control somewhere else – Simon Pollard Apr 29 '16 at 15:39
  • There is a Wordpress plugin integrating git under wordpress : https://versionpress.net/ – Bipi May 04 '16 at 10:10
  • @SimonPollard The question is how to use WordPress automatic updates if WordPress is under version control. Even if you're not using Git, if you're using something like Composer, you still can't use WP automatic updates. With the composer method, you still have to go onto the server and update the WordPress dependency. – Agop May 04 '16 at 14:44
  • I would probably just let the WordPress in production update itself, and then update the dependency in composer the next time I'm deploying. You could also have a cronjob that runs `composer install` to keep your dependencies up to date, and use a loose version string for WordPress that allows minor/major updates depending on your needs. – Steen Schütt Sep 21 '21 at 10:40

2 Answers2

4

It seems the issue is that the "Update Now" button triggers "update the files in production" which are not part of the git repo.

Except they could: the repo git could have the master branch dedicated to the WP usual tree structure as described in WordPress-Skeleton, and a second branch for monitoring the changes to those files in production.

Since git 2.5, you can have multiple worktrees per repo (this is the "out of the box part").
That means you can declare another folder (outside of your original git repo) as a working tree of that same git repo.

Once the update has changed "the files in production", a simple git add -A; git commit can detect said changes and commit them (in a dedicated branch).

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    This talks about the Git side of things, but it doesn't really help much with the WordPress side. Part of the issue is figuring out the best way to perform the commit as part of the WordPress automatic updates (without manually going in after an update and running `git add` and `git commit`). – Agop Apr 27 '16 at 15:48
  • @Agop I understand, but the git side had to be addressed. – VonC Apr 27 '16 at 15:50
4

first of all you do not version control the full WP folder just the themes and plugins folders they won't be affected by the update. So

  • Why is this answer downvoted? It is correct! WP files and folders are part of the deployment process not of the source! If your theme relies on certain plugins to be installed then it either needs to include these plugins inside the theme folder or there needs to be logic that asks the user to install said plugins. – Mario May 04 '16 at 15:14