Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

bash

zsh

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.

like image 504
iain Avatar asked Mar 11 '14 09:03

iain


People also ask

How do I stop a git commit message?

git exit commit message After writing commit message, just press Esc Button and then write :wq or :wq! and then Enter to close the unix file.

What happens if you use the git commit command without specifying a message?

If an empty message is specified with the option -m of git commit then the editor is started. That's unexpected and unnecessary. Instead of using the length of the message string for checking if the user specified one, directly remember if the option -m was given.

How do you ignore Githooks?

Use the --no-verify option to skip git commit hooks, e.g. git commit -m "commit message" --no-verify . When the --no-verify option is used, the pre-commit and commit-msg hooks are bypassed.


1 Answers

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
like image 95
choroba Avatar answered Nov 02 '22 23:11

choroba