Git: pre-commit hook to add modified-file

1. Juni 2016 / Bash / git

×Info: This post is older than 2 years! Displayed information may be outdated!

In einem Repo wurde es notwendig, eine File mit dem letzten „modified“-Datum zu pflegen. Das Datum sollte bei jedem commit automatisch aktualisiert (und mit comitted) werden. Das geht mit git hooks.

# repo und hook erzeugen
git init
touch .git/hooks/pre-commit

# permissions
chmod 775 .git/hooks/pre-commit

Inhalt von pre-commit

#!/bin/bash
#
# A pre-commit hook script to add a "modified.txt"-file with the datetime of the commit

NOW=$(date +"%Y-%m-%d"_%H-%M-%S)
FILE=modified.txt

echo $NOW > $FILE

exec git add $FILE

Info/Nachtrag: mit #!/bin/sh hatte ich Probleme unter Windows. Mit #!/bin/bash tut es bei mir. Git unter Windows ist jetzt nicht mein Haupt-Usecase, aber wenn dannn sollten die Scripte überall funktionieren.