Git Client Side Pre-Commit Hook to stuff current date/time into a file...
For troubleshooting it can be helpful to verify the version of code committed to a system. Once we know the version of code we can check for any bug fixes in newer versions - or determine if this is new exciting bug that needs to be fixed.
In a previous life (Visual SourceSafe) there were Keyword Expansions that could be put in a comment and text would be substituted when it was checked in. I wanted this same functionality in my Git checkins. I didn't find something easy for this... but in true Git style you can get there from here.
In ./git/hooks directory create an executable shell script named pre-commit with the following format:
#!/bin/bash NOW=$(date +"%Y-%m-%d %H:%M:%S") MARKER="-- Last Updated:" for FILE in $(git diff --cached --name-only); do if [ -f "$FILE" ]; then # Replace the entire line starting with the marker sed -i "s/^$MARKER.*/$MARKER $NOW/" "$FILE" git add "$FILE" fi done
Now all lines that have this at the start:
-- Last Updated:
Will be replaced with the current date/time:
-- Last Updated: 2025-10-21 10:35:00
And since the match phrase is just the first part of the line it will refresh it upon every pre-commit phase.
Learn more about Git Hooks here.
Add new comment