Cron – Tracking Crontab Changes with Git

crongit

I would like to use git to track changes in crontab.

I have initialized a new git repository in /var/spool/cron/crontabs/

Now the problem is, when crontab is saved, the second line of the header changes because it contains timestamp.

# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.ubNueW/crontab installed on Thu Aug  1 06:29:24 2019)

What would be the easiest way to ignore these irrelevant changes ?

The possible duplicate question does not address the main point of my question: How to ignore the first 2 irrelevant lines from crontab. Instead, it addresses some other questions which I have not asked, such as some hooks.

Best Answer

You could use a filter:

git config filter.dropSecondLine.clean "sed '2d'"

Edit/create .git/info/attributes and add:

* filter=dropSecondLine

If you don't want the filter acting on all the files in the repo, modify the * to match an appropriate pattern or filename.

The effect will be the working directory will remain the same, but the repo blobs will not have the second line in the files. So if you pull it down elsewhere the second line would not appear (the result of the sed 'd2'). And if you change the second line of your log file you will be able to add it, but not commit it, as the change to the blob happens on add, at which point it will be the same file as the one in the repo.

Related Question