简体   繁体   中英

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?

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."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM