是否可以将 git 提交消息保存到预提交挂钩中的文件中?

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

我正在寻找一种方法来为我的每条提交消息创建带有时间戳的提交消息日志,以此来跟踪我这周所做的事情?有人有这方面的例子吗?

您可以创建 commit.sh bash 脚本来简化该过程。

#!/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 "
git push

## If you want a log of your commits
echo "$NOW - Updated $g from $cv to $nv - " >> [=10=].log

所以你可以:

./commit.sh "I worked a lot on this, now it's done."