Write git commit message before 'git commit'

Solution 1:

Have a look at the -t <file> flag with git commit

This lets you specify a file to use as the basis for the commit message. The editor is still invoked but at least you can use a commit message which you create in advance.

Alternatively, there is another workflow that you can use with git that might better suit your way of working:

With git you can work on a separate branch from the main line and make lots of small commits with their own messages. Although each of these commits by themselves may not solve the problem that you are working on, they do provide a way of saving the intermediate states of your work with the same sort of messages that you may have been updating in your commit message file.

Once you are ready to commit the sum of your work, you can use the rebase command and squash these commits together. Your editor will then be invoked with the all the individual messages that you used for the smaller commits which you can then edit together into a single message.

This is a lot easier than it sounds here, and is IMHO a more git-like approach.

Solution 2:

As long as you haven't push your commit to others, you can do a git commit --amend. This will allow you to modify your commit, as well as your commit message.

I found this really help with the 'commit early and often', without get overwhelm by the number of trivial commits.

Solution 3:

You could use these aliases:

git config --global alias.prepare '!${EDITOR:-vi} $(git rev-parse --git-dir)/.template'
git config --global alias.commitp '!git commit -F $(git rev-parse --git-dir)/.template'

Usage:

git prepare
EDITOR=nano git prepare # heeds standard EDITOR variable
git commitp

This keeps your commit message in .git/.template.

But rather than this, you should really just use a workflow where you commit atomic and small changes often, and use feature branches when necessary to group those changes. If you merge with git merge --no-ff $branch, you can use git log --first-parent later to ignore the branches.