February 14, 2019

Auto Git Commit

Auto Git Commit

Things are ticking along nicely and now you want to maintain a backup of your code and track changes to your service/website/blog, so in the event that things go sideways, it's an easy fix.

So you installed git and started committing things to your personal repositories, but things change even when you aren't actively working on your site or service. Logs and databases are always getting updated and you'd really like to track all those too.

We are going to setup automatic committing with time stamped comments so you always have a view of changes and can always roll things back easily.

This guide assumes you are familiar with the basics of git and have already setup and successfully committed to the repo that you wish to automate.
(If you aren't yet familiar with git here is a good reference to get you started:
https://opensource.com/article/18/1/step-step-guide-git )

You can find my repo for this script here:
https://github.com/AdamGalt/Git-Auto-Commit

Or just grab the code below:

#!/bin/bash
# 
# Script to auto-commit website 
dt=$(date '+%d/%m/%Y %H:%M:%S'); 
cd /var/www/galt.me
git add .
git commit -m "$dt automatic"
git push origin master

All you need to change is the directory that we 'cd' into for your own location.
Then add this script to your crontab

crontab -e

Now paste the line below at the bottom

0 5 * * * /home/adam/scripts/git-galt.sh >/dev/null 2>&1

You simply need to tweak the path to your script location, change the time it runs (if you want) and you are all set!

If you need help with crontab time formatting see:  https://crontab.guru/