10

so I have an sh script that throws together some files then commits them to a git repo. How can I dynamically add the date to my commit message?

My .sh looks something like

// do things to files...
git add -u;
git commit -m 'generated files on <date here?>';
git push origin master;
2
  • 4
    The date is already included in the git log...is it really worth repeating in the message?
    – nneonneo
    Feb 7, 2013 at 0:15
  • True, but it's nice to have it front and center. I also want it to use server time. Feb 7, 2013 at 0:15

3 Answers 3

34

Just format the output of the date command and Bob's your uncle:

// do things to files...
git add -u;
git commit -m "generated files on `date +'%Y-%m-%d %H:%M:%S'`";
git push origin master
3
  • This added the entire string, including the ticks Feb 7, 2013 at 0:18
  • My apologies for the quote issue. Mistyped.
    – hd1
    Feb 7, 2013 at 0:24
  • Thanks. This was helpful for the alias that I created (which I talk about at stackoverflow.com/q/64823742/470749): function stpu() { git stash push -u -m "`date +'%Y-%m-%d %H:%M:%S'` ${@}" }
    – Ryan
    May 5 at 16:11
3

Why not use prepare-commit-msg or commit-msg git hooks? You can find stubs in your .git/hooks directory.

1
  • 1
    Best answer imo.
    – coffman21
    Mar 20, 2019 at 9:27
2

Not sure why you'd do that since commits are already timestamped, but something like:

THEDATE=`date`
git commit -m "... $THEDATE"

would do so. Note that the double-quotes are important.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.