Supplying Git a commit message prepared ahead of time
Friday, 28 Mar 2025 [Tuesday, 1 Apr 2025]
Say you want to write script that will set up the next Git commit for you without creating it – just stage some content and prepare a commit message. The next bare git commit
should automatically populate the editor with that message, without any flags passed to it, nor any Git configuration changes, and without affecting any subsequent invocations of git commit
. The staging part is obvious… but where do you put the commit message?
The answer, which appears to be undocumented, turns out to be .git/MERGE_MSG
:
echo 'there we go' > .git/MERGE_MSG
If you now run git commit
, your editor will open with the “there we go” commit message already entered.
Update: originally in this entry I suggested using .git/SQUASH_MSG
, which probably seems more eccentric than .git/MERGE_MSG
. Because I could not remember how to trigger this behaviour in Git outside of a merge or rebase, I was reduced to reading the code of git commit
, where the related logic is somewhat convoluted, and the .git/SQUASH_MSG
case is the easiest to discover. I finally remembered that it is git cherry-pick --no-commit
where I saw this behaviour first, which allowed me to discover what it does by just running the command and examining the .git
directory. I am writing this down because that should remain an easy route to figuring out whatever the new mechanism is, should it ever change.