Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to save a git commit message to a file in a pre-commit hook?

I am looking for a way to create a log of commit messages with timestamps for each of my commit messages as a way to keep track of what I've done for the week? Does anyone have an example of this?

like image 462
geschwe1 Avatar asked Mar 26 '26 12:03

geschwe1


1 Answers

You can create a commit.sh bash script to simplify the process.

#!/bin/bash

## your timestamp
NOW=$(date +%Y.%m.%d-%H:%M:%S)

## your source file
g=main.sh

## get the current version by extracting from the first 3 lines 
## ideally they are comments, containing the version
cv=`head -n 3 $g | grep "version" | sed 's/[^0-9.]*//g'`

## if you want a new version automatically incremented
nv=`echo $cv | awk -F. -v OFS=. 'NF==1{print ++$NF}; NF>1{if(length($NF+1)>length($NF))$(NF-1)++; $NF=sprintf("%0*d", length($NF), ($NF+1)%(10^length($NF))); print}'`

## automatically apply it in your sourcecode
echo "Updateing $g from $cv to $nv $NOW"
## re-print the first 3 lines of your code - this is bash here
cat $g > $g.bak
echo '#!/bin/bash' > $g
echo '# Last update:'$NOW >> $g
echo '# version '$nv >> $g
tail -n +4 $g.bak >> $g
rm $g.bak

## git action
git add . --all
git commit -m "$nv $1"
git push

## If you want a log of your commits
echo "$NOW - Updated $g from $cv to $nv - $1" >> $0.log

So you can:

./commit.sh "I worked a lot on this, now it's done."
like image 134
Király István Avatar answered Mar 29 '26 09:03

Király István