0

I repeatedly caught myself calling git log before git commit to see what were the last commits before making a new one.

Is there a way how to automatically populate the commented-out section of the git commit message in the editor with the output of git log? Perhaps even with some options like -n5? Right now, it shows the branch and staged files.

Very similar with git: Show index diff in commit message as comment question but --verbose does not show the logs.

I use vim as my editor if it is relevant.

Quimby
  • 17,735
  • 4
  • 35
  • 55
  • 1
    You can put your cursor anywhere in the commented-out section and do `:read !git log -n5`. See `:help :read`. – romainl Aug 06 '21 at 12:35
  • @romainl Interesting solution, it could be even coupled with autocommand on opening the git message file to make it automatic. Thank you. – Quimby Aug 06 '21 at 12:57
  • You can use a `prepare-commit-msg` hook to edit the commit message file in place before the editor opens, too. This lets you write arbitrarily complex scripts. – torek Aug 06 '21 at 14:16

1 Answers1

2

The commit message can be customised using the --edit and --file options. A hyphen can be used with the --file option to read additional commit message text from stdin. This didn't work well for me with vim; maybe because it was not invoked as vim -.

Bash process substitution seems to work though:

git commit --edit --file=<(git log -n5 | sed 's/^/# /')
Cole Tierney
  • 9,571
  • 1
  • 27
  • 35
  • Thank you, this is exactly what I was looking for, I will just add an alias for so I do not have to type it every time. – Quimby Aug 06 '21 at 13:00