.gitignore for wordpress development

Without a correctly set .gitignore file during site development using wordpress, thousands of files are unnecessarily tracked. WordPress and wordpress plugins can be updated frequently, often with updates a site developer has no concern in keeping track of. Most site development occurs in the ‘wp_content/themes’ folder. Inserting a .gitignore file to specify files/folders to ignore is an obvious first step. 

GitKraken’s .gitignore wordpress template is a good basis to start with… GitKraken .gitignore template for wordpress

I make some modifications to match my workflow. I have found that tracking plugin development should be done separately and so I ignore all folders except the theme(s) I am working with. Plugins get their own separate repositories…

Here is a .gitignore file that I have modified to suit:

# ignore everything in the root except the "wp-content" directory.
/*
!wp-content/

# ignore everything in the "wp-content" directory, except:
# "themes" directory
wp-content/*
!wp-content/themes/

# ignore specific themes
wp-content/themes/twenty*/
!wp-content/themes/current-custom-theme/

# ignore node dependency directories
node_modules/

# ignore log files and databases
*.log
*.sql
*.sqlite

It is best to get the .gitignore file set up early on (ie the first step) when initiating git. If files are accidentally included, they will still be included until they are removed. To remove incorrectly tracked files, all files are cleared and then ‘re-added’. Hence the importance of getting it correctly setup initially. Here is an example of clearing the tracked files in git and re-adding them:

git rm -r --cached .
git add .
git commit -m ".gitignore is now working"

Leave a Reply

Your email address will not be published.