简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM