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.
thanks
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
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 -
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.
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"