RSSAmplifier

~talfus-laddus - Blog · Jul 5, 2026

How to add previous commit messages and authors to you Git commit template?

0
Sign in to vote or save

This page cannot be shown here. You can still read it on the original site — the toolbar below keeps your place in the directory.

I find having the context of the previous commit messages helpful when writing a new commit message. Especially when using Scoped Commits , it helps seeing scoped used in the past. Git comes with a customizable message template, but it is static. What is needed is a wrapper around git commit to fetch the latest git commit messages, and to prepend this text to the current git message file. To wrap…

I find having the context of the previous commit messages helpful when writing a new commit message. Especially when using Scoped Commits</a>, it helps seeing scoped used in the past.</p>

Git comes with a customizable message template, but it is static. What is needed is a wrapper around git commit</code> to fetch the latest git commit messages, and to prepend this text to the current git message file.</p>

To wrap our git commit</code>, we can simply change our editor to a script:</p>

[core]</span></span>
editor = git-commit-wrapper.sh</span></span></code></pre>

The script that replaces our editor gets one argument: the commit message file</em> with text from the git message template.</p>

In this script, we can now dynamically run every command we want to get the context need, and put it into the commit message file before opening the file in our editor.</p>

Currently, my git commit wrapper looks like this:</p>

#!/bin/sh</span></span>
set</span> -e</span></span>
</span>
COMMIT_MSG_FILE</span>="</span>$1</span>"</span></span>
</span>
# Show the latest 10 commits</span></span>
{</span></span>
	echo</span> ""</span></span>
	echo</span> "</span># Recent commits:</span>"</span></span>
	git</span> log</span> --pretty=format:</span>"</span>%s</span>"</span> -n 10</span> | while</span> read</span> -r</span> line</span>; do</span></span>
		echo</span> "</span># </span>$line</span>"</span></span>
	done</span></span>
} >>"</span>$COMMIT_MSG_FILE</span>.tmp</span>"</span></span>
</span>
cat</span> "</span>$COMMIT_MSG_FILE</span>" >>"</span>$COMMIT_MSG_FILE</span>.tmp</span>"</span></span>
mv</span> "</span>$COMMIT_MSG_FILE</span>.tmp</span>" "</span>$COMMIT_MSG_FILE</span>"</span></span>
</span>
$EDITOR</span> "</span>$COMMIT_MSG_FILE</span>"</span></span></code></pre>

Another nice feature that comes with most IDEs is the suggestion of co-authors. Neovim does not have this features. Giving our wrapper above its simple to extent it with previous authors.</p>

#!/bin/sh</span></span>
set</span> -e</span></span>
</span>
COMMIT_MSG_FILE</span>="</span>$1</span>"</span></span>
</span>
# Show the latest 10 commits</span></span>
{</span></span>
	echo</span> ""</span></span>
	echo</span> "</span># Recent commits:</span>"</span></span>
	git</span> log</span> --pretty=format:</span>"</span>%s</span>"</span> -n 10</span> | while</span> read</span> -r</span> line</span>; do</span></span>
		echo</span> "</span># </span>$line</span>"</span></span>
	done</span></span>
} >>"</span>$COMMIT_MSG_FILE</span>.tmp</span>"</span></span>
</span>
# Suggest the last 5 authors as co-authors</span></span>
{</span></span>
	echo</span> ""</span></span>
	echo</span> "</span># Recent authors:</span>"</span></span>
	git</span> log</span> --format=</span>'</span>%an <%ae></span>'</span> -n 5</span> |</span> sort</span> -u</span> | while</span> read</span> -r</span> author</span>; do</span></span>
		echo</span> "</span># Co-authored-by: </span>$author</span>"</span></span>
	done</span></span>
} >>"</span>$COMMIT_MSG_FILE</span>.tmp</span>"</span></span>
</span>
cat</span> "</span>$COMMIT_MSG_FILE</span>" >>"</span>$COMMIT_MSG_FILE</span>.tmp</span>"</span></span>
mv</span> "</span>$COMMIT_MSG_FILE</span>.tmp</span>" "</span>$COMMIT_MSG_FILE</span>"</span></span>
</span>
$EDITOR</span> "</span>$COMMIT_MSG_FILE</span>"</span></span></code></pre>

Read on /blog/git-commit-wrapper/

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.