iain
iain

Reputation: 16274

How do I disable being able to set the git commit message from the command line?

I find that being able to specify the commit message in one go, tricks me into writing short one line commit messages. I often end up along the lines of git commit -m "fix things". But whenever I leave off the -m option, and my editor pops up, I'm more likely to write a good commit message.

In the past I've created habits by disabling features I didn't want to use anymore. As example: I disabled the arrow keys in vim, which finally made me use hjkl. This was so effective, I want to try to do the same for the git commit messages. I want git (or bash or zsh) to yell at me for trying to use commit -m.

I could write a wrapper around the git command entirely, but maybe you have other solutions and I might learn something cool! I'm open to all sorts of magic and trickery.

Upvotes: 7

Views: 1469

Answers (1)

choroba
choroba

Reputation: 241828

You can create a prepare-commit-msg hook in your .git/hooks directory to check the length of the message and reject the commit if the message is too short:

#!/bin/bash
# Remove whitespace, at least 50 characters should remain.
if (( $(sed 's/\s//g' "$1" | wc -c) < 50 )) ; then
    echo Message too short. >&2
    exit 1
fi

Upvotes: 6

Related Questions