0

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,294
  • 1
  • 19
  • 44
  • 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
  • Does this answer your question? [Add line break to 'git commit -m' from the command line](https://stackoverflow.com/questions/5064563/add-line-break-to-git-commit-m-from-the-command-line) – user14464173 Mar 25 '23 at 16:20

4 Answers4

5

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
  • 19,645
  • 3
  • 36
  • 61
Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
  • 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
2

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
  • 9,360
  • 2
  • 33
  • 47
  • 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
1

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

git commit --file ~/proj/commit-message

It may be convenient to have a persistent message file (commit-message) in the starting directory of your projects (~/proj), this allows to use the same command repeatedly.

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
  • 55,082
  • 5
  • 77
  • 137