Preparing a git commit message before committing?

Viewed 26658

Is it possible to prepare a commit message prior to a commit, meaning that I will type my commit message before the actual commit or during the working dir so that I know what I am working on and I know what this branch or commit will be all about. When I say "before" I do not mean just couple secs before I enter the commit on the commandline. I literally mean right after a commit or at the starting of a branch so that the next commit will automatically inherit the message in the queue or what ever that might be called.

Naturally I can put these messages during the commit, to me there is a difference. And I can see the argument of well git is not meant for that as well. I am just curious about it.

I also know I can give my branches more meaningful names but I just want a bit space for such purpose.

6 Answers

You can also get git's vim formatting (excluding the comment lines that usually list the included/excluded changes) by using this command:

vi -c 'set syntax=gitcommit'

I made an alias (non-git) and added it to my ~/.bashrc.

alias ecm="vi -c 'set syntax=gitcommit'"

Re-source your .bashrc to turn it on for your current session.

source ~/.bashrc

And then use it.

ecm message.txt

All this was done with Git Bash on Windows.

I combined the answers of LopSae and janos to auto remove all lines beginning with a # from the input-textfile with command from this post.

Result is this command

cat .git/COMMIT_EDITMSG | sed '/^#/ d' | git commit -F -

And to make it shorter I suggest to add an alias.

EDIT Creating an alias for this command was more complex than exspeced. But after some tries and withs this tuturial I created an alias what works and supports custom parameters.

[alias]
  commitmsg  = "!f() { myarg=${@}; cat .git/COMMIT_EDITMSG | sed '/^#/ d' | git commit -F - $myarg;  }; f"

You can use it e.g. with parameter for modified date like this

commitmsg --date "2001-01-02 18:00:00"

You could add a template file to your project's git configuration and its content will be used each time you start a new commit. This file might be added to .gitignore so that its existence will be only relevant for you while developing. Standing in the root of your project, you could for example do:

touch .gitmessage
echo "\n# commit message\n.gitmessage" >> .gitignore
git config commit.template .gitmessage

Hope this helps someone else in the future!

Related