10

How do I add multi-line messages using

git commit -a -m "..."

The answer from this similar question appears to work (judging by the upvotes and acceptance), but it seems a bit cumbersome1.

The git documentation reads:

-m <msg>
--message=<msg>
Use the given <msg> as the commit message. If multiple -m options are given, their values are concatenated as separate paragraphs.

So, would it be advisable to just use a new -m for each new line of the message? Or do "paragraphs" add additional line spacings?

It would be nice if future versions of the command would allow us to just add \n between sentences, to denote line breaks.

1 The linked answer basically advises to use a message template file, and direct git to use the file via git commit -t <template_file>

Blair Fonville
  • 908
  • 1
  • 11
  • 23
  • 1
    See some of the answers [here](https://stackoverflow.com/q/24587262/4276832). Basically, it seems like if you omit the `-m` entirely, your default editor (vim, nano, etc.) will open and you can enter however many lines of commit message you want. – bsinky Jan 24 '18 at 20:04
  • If you omit the `-m`, your favorite editer will open wich will allow you to type a muti line commit message. – Matt Clark Jan 24 '18 at 20:04
  • @bsinky Thanks, I do know about omitting `-m` to add the messages in the editor. I was wondering if there was a simple solution to do it straight from the command line. – Blair Fonville Jan 24 '18 at 20:06
  • In addition to omitting `-m`, you can also start your message with a `"` and then just hit `enter`. The message will continue across newlines until you terminate with another `"` Example: ``` git commit -m "line1 line2 line3" ``` – torbinsky Jan 24 '18 at 20:08
  • 1
    @torbinsky - *if* they are using bash, that is true. Not if they are using CMD on windows, for example. This is shell-specific behavior. (But it is also the approach I point out in my answer, because bash is definitely a good shell to use for command-line git operations.) – Mark Adelsberger Jan 24 '18 at 20:14
  • @MarkAdelsberger Agreed. I tested your answer on both (bash and Windows cmd), and it only worked in the bash shell. I don't think this convenience is enough to get me to start using the bash shell on Windows. When I need linuxy stuff, I use a VM. On Windows, I use Windows. – Blair Fonville Jan 24 '18 at 20:26
  • 3
    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) – Daemon Painter Apr 24 '20 at 09:01
  • I answered this in https://stackoverflow.com/a/46645586/6839738 – blongho Nov 29 '20 at 12:18
  • try this: ```git commit -m "line 1 comment" -m "line 2 comment" ``` – ivsuleman Feb 10 '22 at 16:12

2 Answers2

22

The answer may depend on what shell you use to run git. For example with bash (just tested this on windows using the bash shell installed with git):

git commit -m "this is
a multi-line
message"

because quite simply bash will not assume that hitting return ends the command if it's in the middle of a quoted string.

That said, I've only ever used -m for one-line messages; other options just "make more sense" to me if I need a multi-line message.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
  • I am on Windows. Just tested it with git bash shell, and it worked as advertised (+1). Unfortunately, I never use the bash shell. It's overkill for me, and I can't then use all of my batch scripts. I also tested it from a Windows command prompt and (as you assumed) it doesn't work there unfortunately. – Blair Fonville Jan 24 '18 at 20:20
  • It seems that Powershell on Windows will let you enter multi-line messages in this way. – bsinky Jan 24 '18 at 20:59
-3

use the following commands

  1. git commit
  2. ctrl+c
  3. Press I
  4. Type multi line comment
  5. Press ctrl+c
  6. :wq
Irfan Ul Haq
  • 1,065
  • 1
  • 11
  • 19