Friday, January 7, 2011

Git commit mode

Hardly anything ruins a glorious day of coding like fat-fingering the commit message late at night as you doze off, and then pushing it out for the world to see. To prevent that, I have equipped my Emacs configuration with a few little tools now.

First, I found the git-commit-mode, a special mode for Git commit messages. This helps you format the commit messages according to convention, and will use ugly colors if, for example, you write lines that are too long or you do not keep the second line blank. It also allows the use of things like M-q without shredding the whole file template.

Second, I integrated on-the-fly spell checking into the git-commit-mode. It won't stop you from writing nonsense, but it will catch the silly mistakes.

Here's a simple configuration snippet:
(require 'git-commit)
(add-hook 'git-commit-mode-hook 'turn-on-flyspell)
(add-hook 'git-commit-mode-hook (lambda () (toggle-save-place 0)))
The last line is handy if you have save-place on by default. When you make a new commit, it would then normally place the cursor where a previously edited commit message was finished, because to the save-place functionality, it looks as though it's the same file.

3 comments:

  1. As long as you have only committed locally and haven't pushed it anywhere - you can also use "git commit --amend" to modify the latest commit.

    Not only can you change the commit message, you can make other changes too - which is handy if you forgot to update the changelog for example.

    ReplyDelete
  2. I'm glad you like git-commit-mode.

    If you can find a way to incorporate your toggle-save-place trick in a way that won't break things for people not using save-places, I'd be happy to integrate that so you can get rid of that mode hook.

    Also, turning on flyspell-mode is one of the things I want to happen automatically for git-commit-mode as well. But also there's tons of other cases where I want flyspell-mode depending on the mode of the buffer I'm editing, or any other condition. In order to not write mode hooks for every single mode I'm using and hardcode things in there, I've written espect.el. You might want to give that a try as well :-)

    ReplyDelete
  3. thanks for the post. I used diff-mode for git commit messages so far (I often use git commit -v) but M-q does not work as expected without switching modes there. I'll give git-commit-mode a try.

    ReplyDelete