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;
Steve Robbins
  • 13,672
  • 12
  • 76
  • 124

3 Answers3

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
hd1
  • 33,938
  • 5
  • 80
  • 91
  • This added the entire string, including the ticks – Steve Robbins Feb 07 '13 at 00:18
  • My apologies for the quote issue. Mistyped. – hd1 Feb 07 '13 at 00:24
  • Thanks. This was helpful for the alias that I created (which I talk about at https://stackoverflow.com/q/64823742/470749): `function stpu() { git stash push -u -m "\`date +'%Y-%m-%d %H:%M:%S'\` ${@}" }` – Ryan May 05 '23 at 16:11
3

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

madhead
  • 31,729
  • 16
  • 153
  • 201
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.

cjc343
  • 3,735
  • 1
  • 29
  • 34