git commit accepts several message flags (-m) to allow multiline commits
- Published at
- Updated at
- Reading time
When you use git on the command line you might have used the message flag (
-m). It allows developers to define commit messages inline when calling git commit.
git commit -m "my commit message"
I'm not the biggest fan of this approach because I prefer to edit the commit message in vim (which I only use for writing commit messages). It gives me the opportunity to double-check the files I'm committing.
Today I learned that the git commit command accepts multiple message flags. 😲
It turns out that you can use the
-m option multiple times. The git documentation includes the following paragraph:
If multiple -m options are given, their values are concatenated as separate paragraphs
If you run the following command
git co -m "commit title" -m "commit description"
it will result in this commit.
Author: stefan judis <firstname.lastname@example.org> Date: Tue Jul 7 21:53:21 2020 +0200 commit title commit description test.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-)
You can use multiple
-m flags to create "multiline commits", and I have to admit that this can be very handy in some cases.
Edited: Several people pointed out that you can achieve the same commit structure including a title and body (multiple lines) by opening quotes, pressing enter and closing the commit with quotes again.
git commit -m "commit title > > commit description" [master 2fe1ef8] commit title 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test-2.txt
If you want to see this command in action, I shared a short terminal session on Twitter with a little video.
And thanks to Stephan Schneider who shared that little git tip in our company slack. 🙇♂️