How to use WordPress with Git efficiently

I use to work with frameworks like Symfony, Angular, Laravel and more.

When it comes to VCS (Version Control System), we need to remind some facts.

WHAT ARE THE ADVANTAGES OF VCS
1. Keep track of changes by owners, time…
2. Easily roll back changes when necessary
3. Easier to collaborate with other developers

WHAT IS NOT VCS MADE FOR
1. Replacement for scheduled backups, VCS !== BACKUP !!!
2. Debugging. Since every little change is tracked, it’s not its purpose.
3. Committing untested or broken code to the repository… Just a matter of sense !
4. Committing generated content (from user or otherwise)
5. Uploads & thumbnails => heavy repo with absolutely no benefit, use a cdn for media content !

WHAT IS THE GOAL OF VCS WITH WORDPRESS (Like another framework or cms)
1. Keep sensitive information out of the repository
2. Avoid wordpress core and upload files

Ok. You will surely tell me that this way, it will be a pain to retrieve the full project for a new developer, because there will be no wordpress core files, nor wp-config.php file.

Sure you’re right. But remember, VCS !== BACKUP, keep third parties you are not allowed to touch apart of your repository.
ANND, I will show an easier way to bypass this simple issue.

  • It is not a good way to modify wordpress core (what about future updates)? Don’t keep core files inside your repository.
  • Config file is personal to each user, never shared it on vcs, keep database datas secret.

NOW, LET’S START !!!!

Let’s start with the .gitignore file content

# Ignore everything in the root except the "wp-content" directory.
/*
!.gitignore
!wp-content/
!.htaccess

# Ignore everything in the "wp-content" directory, except the based themes and the "tmp" directory.
wp-content/themes/twenty*
wp-content/tmp/

#Do this essentially if you store your media content inside a cdn
wp-content/uploads/ 

# Hidden files
*.DS_Store
*Thumbs.db
*.sass-cache*
*~imageoptim*

Now, let’s grab the wordpress core and config files. For that we will need “wp-cli” extension, you can download it here: https://www.howtoforge.com/tutorial/getting-started-with-wp-cli/

Once, you have “wp-cli” enabled in your cli environment, we can start the fun:

  1. Install WordPress core files with a specific version
    wp core download --version=x.x.x --allow-root
    
  2. Install config file with database datas

    wp core config --dbname=mysite --dbuser=root --dbpass=password --dbhost=127.0.0.1
    

And here we go, quite simple, enjoy 🙂

How to use WordPress with Git efficiently