Auto-Pull a Git Repository with Cron
To keep a deployment or content checkout in sync, run git pull from cron every few minutes. The two gotchas are running as the repo's owner and giving cron access to the SSH key or a read-only HTTPS token.
The crontab line
*/5 * * * * cd /var/www/site && /usr/bin/git pull --ff-only >> /var/log/git-pull.log 2>&1
Runs every 5 minutes. --ff-only refuses to create a merge commit, so a local change or diverged history fails loudly instead of leaving a half-merged tree.
Run as the repo owner
Put the job in that user's crontab (crontab -e as them), not root, so file ownership stays correct.
SSH key access
Cron has no ssh-agent. Either use an HTTPS remote with a stored token, or add GIT_SSH_COMMAND='ssh -i /home/me/.ssh/deploy_key' before the git command so it finds the key.
FAQ
What cron line auto-pulls a git repo every 5 minutes?
*/5 * * * * cd /repo && git pull --ff-only >> pull.log 2>&1 pulls every 5 minutes and logs output. --ff-only prevents accidental merge commits on diverged history.
Why does git pull fail silently in cron but work in my shell?
Cron has no ssh-agent and a minimal environment. Use an HTTPS remote with a token, or set GIT_SSH_COMMAND with -i pointing at the deploy key so git can authenticate.
Which user should run a git-pull cron job?
The user that owns the checkout. Add the job with crontab -e as that user so pulled files keep the right ownership and permissions.