Preparing a git commit message before committing?

Git

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

Git can take the commit message from a file using the -F or --file flags:

git commit -F message.txt

You can prepare your message in advance in a text file and use that file when you commit.

If you do this often, it makes sense to create an alias for it, for example:

done = commit -F message.txt

So that you can simply type git done to have it always use your text file.

If you make a mistake and commit too fast without updating the message file, not a problem, you can just do git commit --amend and fix the message in the commit.

UPDATE

The -e flag is useful too, as it lets you edit the message before committing:

git commit -eF message.txt

Solution 2 - Git

If using the --file option for git commit you can pipe in the message through the standard input by using dash (-) instead of a file name.

echo "Classy commit message" | git commit --file -

Solution 3 - Git

If you're using VIM as Git's core editor, then when you run git commit, VIM will be opened and you'll be presented with a buffer containing the commented-out output of the git status command.

You can then use the VIM command :read COMMIT_MSG.txt which will insert the contents of the file COMMIT_MSG.txt (your pre-pared commit message) at the current cursor location.

This is really just an alternative to running git commit -eF COMMIT_MSG.txt, but I personally find it easier to remember the VIM command :read as opposed to having to remember yet another git command line argument. Personal preference, really.

Solution 4 - Git

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.

Solution 5 - Git

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"

Solution 6 - Git

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!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionyarun canView Question on Stackoverflow
Solution 1 - GitjanosView Answer on Stackoverflow
Solution 2 - GitMaic López SáenzView Answer on Stackoverflow
Solution 3 - GitsverschView Answer on Stackoverflow
Solution 4 - GitChrisView Answer on Stackoverflow
Solution 5 - GitRadon8472View Answer on Stackoverflow
Solution 6 - GittebanepView Answer on Stackoverflow