git push to github via cron on mac

TL;DR: Solved problems with git push via cron by using SSH instead of HTTPS.

Thanks to the support team at GitHub I was able to solve this.

The first problem was that git-credential-osxkeychain was not in my path when the cron job ran. For me, git and git-credential-osxkeychain were both being run out of the Xcode command line tools directory:

$ which git
/Applications/Xcode.app/Contents/Developer/usr/bin/git
$ which git-credential-osxkeychain
/Applications/Xcode.app/Contents/Developer/usr/bin/git-credential-osxkeychain

When running which git from cron, turns out cron was running git from /usr/bin.

I added the appropriate directory to my PATH at the top of the cron job with:

export PATH=$PATH:/Applications/Xcode.app/Contents/Developer/usr/bin

That solved the git: 'credential-osxkeychain' is not a git command error, but I got yet another error:

fatal: could not read Username for 'https://github.com': Device not configured

At the suggestion of the GitHub support team, I solved the problem (without the need for git-credential-osxkeychain at all) by authenticating via SSH instead of https:

git remote set-url origin [email protected]:username/repo.git