-2

How do I produce a multiline comment in my git commit using -m

$ git commit -m "This change is for blah"
$ git commit -m "This change is for blah\nAnd also for this blah"

The second line, the \n doesn't produce a multiline comment

I'm using Mac terminal, not sure if that matter.

RonPringadi
  • 1,064
  • 1
  • 12
  • 34
  • and this: https://stackoverflow.com/questions/30423751/how-to-do-a-multiline-commit-message-in-git – Chris Maes Oct 18 '19 at 14:52
  • 1
    `git commit` doesn't do any processing of the argument to `-m`; it's just a single string. Any method of embedding newline characters in that string will depend on how you specify the argument. – chepner Oct 18 '19 at 14:59

4 Answers4

4

In many environments (MacOS included) you can just hit enter to end the first line, as long as the quotes for the message are still open.

git commit -m "this is
a multiline
message"

Another option is to compose the message in a file and use -F. (This is a more scriptable alternative to letting a text editor open for the commit message.)

The "multiple -m option' approach others are suggesting kinda-sorta works, but puts blank lines between the messages in my tests.

Romain Valeri
  • 17,132
  • 3
  • 30
  • 49
Mark Adelsberger
  • 36,896
  • 2
  • 27
  • 48
  • 1
    Using `-m` more than once is documented to create separate *paragraphs* in the commit message, not just separate lines, hence the blank lines. – chepner Oct 18 '19 at 15:01
1

Consider using --file instead of -m. You can prepare the message in a file and then commit like this:

git commit --file=../commit_message
0

If you just use

git commit

It will open your editor and allow you to add more complex commit messages.

If you use

git commit -m "This is for blah" -m "This is more text" 

it should commit several lines of text.

Kenny Grant
  • 8,790
  • 2
  • 30
  • 45
  • 2
    Multiple uses of `-m` will commit several *paragraphs*, not just several lines. – chepner Oct 18 '19 at 14:58
  • @chepner that's usually desired, as the convention is to have a blank line after the summary line and the summary line should be a single line and on the shorter side. – siride Mar 25 '22 at 17:46
0

This depends on your shell, most shells have a process-C-escapes marker. bash and I think many others use $''.

git commit -m subject -m $'this\nhas\nexplicit\nlinebreaks'

Just a note, git log can do word wrapping for you anyway, try

git log --pretty='%h%+w(76,6,9)%B'

for starters.

jthill
  • 48,781
  • 4
  • 72
  • 120