Git: Ignore WordPress Cache Files using .gitignore

I’ve had a nagging problem for some time where I couldn’t figure out quite how to ignore my wp-content/cache directory when doing syncs with git. It’s not that it was a lot of network traffic, or a lot of storage being used; it’s just messy to sync those cache files every time you make a change to your site–especially since they’re constantly being added and deleted.

So I finally figured out the syntax, and it’s actually quite simple: you just give the relative path of the directory you want to ignore–from the perspective of your root repository. So I work out of my htdocs directory for managing my website, which means you simply add the following to your .gitignore file, which is placed right in the root of htdocs.

cat.gitignore

/wp-content/cache/

Keep in mind, however, that this will only keep you from adding new content from that directory, so in order to purge existing cache files from the repository you need to manually remove them:

git rm -rf –cached wp-content/cache

The --cached piece is important; if you don’t add that you’ll not only remove the files from the git repository, but also from the file system.