Since Git can currently only stage files and not empty directories, the only purely Git based solution is to do as you are now.
An alternative approach is to include a bash script in your repo that creates any required cache directories if the do not exist and (importantly) sets permissions on these directories (which often needs to be 755/777).
The other answers are going about this a slightly convoluted way. You don't need a special .gitignore in each folder, and that makes it hard for other developers to figure out what's going on (or you in a few months).
Just ignore the whole cache folder in your root .gitignore:
$ cd /path/to/root
$ echo "system/expressionengine/cache/" >> .gitignore
A possibly cleaner way would be to have it in your .gitignore at the root level of your repo...
... that way everything's centralized in that one .gitignore. However it's my preferred method to have multiple .gitignore files (like your example above), one in each "empty" directory ...
Like Bhashkar said, that code should work in either a PHP-enabled template or put in a plugin and then put in a template. Then call the template to clear cache.
A more advanced way, and probably the proper way to do it, is to create a simple module, which can use an Action URL. Stephen Lewis wrote a blog post about what Actions are. Basically, an action ...
We use a single .gitignore file in the root directory, and we specifically list all subfolders. This way, the cache folder can contain the default index.html file, and will be indexed by git.
You probably got errors because EE couldn't find the cache folder. I'm new to git, and I have all my ignore rules in one file in the root and there I have this
Jacob, it occurs to me that CE Cache could come to the rescue here, perhaps in more than one way.
If you are able by intended site content patterns to use it in the sense normally intended, and choose for example the easiest Static Caching mode, you'll have EE actually not serving most of the page hits -- they'll come straight from CE's cache of precompiled ...
If a chmod 777 won't fix it, it could be a directory/file ownership issue.
First try letting OSX fix it. Open up Disk Utility app on your Mac, click on your primary partition (under your primary drive) and click "Repair Permissions".
If that doesn't work, you'll need to do a little homework to figure out the correct folder/file ownership permissions for ...