Search code examples
gitvimgit-commit

How to show `git log` in the editor during `git commit`?


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.


Solution

  • 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/^/# /')