Add line break to 'git commit -m' from the command line

GitBashShell

Git Problem Overview


I am using Git from the command line and am trying to add a line break to the commit message (using git commit -m "") without going into Vim.

Is this possible?

Git Solutions


Solution 1 - Git

Certainly, how it's done depends on your shell. In Bash, you can use single quotes around the message and can just leave the quote open, which will make Bash prompt for another line, until you close the quote. Like this:

git commit -m 'Message

goes
here'

Alternatively, you can use a "here document" (also known as heredoc):

git commit -F- <<EOF
Message

goes
here
EOF

Solution 2 - Git

If you just want, say, a head line and a content line, you can use:

git commit -m "My head line" -m "My content line."

Note that this creates separate paragraphs - not lines. So there will be a blank line between each two -m lines, e.g.:

My head line

My content line.

Solution 3 - Git

Using Git from the command line with Bash you can do the following:

git commit -m "this is
> a line
> with new lines
> maybe"

Simply type and press Enter when you want a new line, the ">" symbol means that you have pressed Enter, and there is a new line. Other answers work also.

Solution 4 - Git

Adding line breaks to your Git commit

Try the following to create a multi-line commit message:

git commit -m "Demonstrate multi-line commit message in Powershell" -m "Add a title to your commit after -m enclosed in quotes,
then add the body of your comment after a second -m.
Press ENTER before closing the quotes to add a line break.
Repeat as needed.
Then close the quotes and hit ENTER twice to apply the commit."

Then verify what you've done:

git log -1

You should end up with something like this:

Multi-line Git commit message in PowerShell

The screenshot is from an example I set up using PowerShell with Poshgit.

This works in all terminals and Operating Systems AFAIK.

Here's an example with bash:

Multi-line commit message in git

Resulting in this commit:

Multi-line commit

Solution 5 - Git

You should be able to use

git commit -m $'first line\nsecond line'

From the Bash manual:

> Words of the form $'string' are treated specially. The word expands to > string, with backslash-escaped characters replaced as specified by the > ANSI C standard.

This includes support for newlines as shown above, plus hex and Unicode codes and others. Go to the linked section to see a list of the backslash-escaped characters.

Solution 6 - Git

There is no need complicating the stuff. After the -m "text... the next line is gotten by pressing Enter. When Enter is pressed > appears. When you are done, just put " and press Enter:

$ git commit -m "Another way of demonstrating multicommit messages:
>
> This is a new line written
> This is another new line written
> This one is really awesome too and we can continue doing so till ..."

$ git log -1
commit 5474e383f2eda610be6211d8697ed1503400ee42 (HEAD -> test2)
Author: ************** <*********@gmail.com>
Date:   Mon Oct 9 13:30:26 2017 +0200

Another way of demonstrating multicommit messages:

This is a new line written
This is another new line written
This one is really awesome too and we can continue doing so till ...

[EDIT 05-05-2021]

For Windows users, use GitBash for Windows. The built-in Windows cmd does not work with this method.

Solution 7 - Git

From Git documentation:

> -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, if you are looking for grouping multiple commit messages this should do the work:

git commit -m "commit message1" -m "commit message2"

Solution 8 - Git

I hope this isn't leading too far away from the posted question, but setting the default editor and then using

git commit -e

might be much more comfortable.

Solution 9 - Git

Doing something like

git commit -m"test\ntest"

doesn't work, but something like

git commit -m"$(echo -e "test\ntest")"

works, but it's not very pretty. You set up a git-commitlb command in your PATH which does something like this:

#!/bin/bash

message=$1

git commit -m"$(echo -e "$message")"

And use it like this:

git commitlb "line1\nline2\nline3"

Word of warning, I have a feeling that the general convention is to have a summary line as the first line, and then two line breaks, and then an extended message in the commit message, so doing something like this would break that convention. You could of course do:

git commitlb "line1\n\nline2\nline3"

Solution 10 - Git

In Bash/Zsh you can simply use literal line breaks inside quotes:

git commit -m 'Multi-line
commit
message'

ANSI-C quoting also works in Bash/Zsh:

git commit -m $'Multi-line\ncommit\nmessage'

You can also instruct Git to use an editor of your choice to edit the commit message. From the docs on git-commit:

> The editor used to edit the commit log message will be chosen from the > GIT_EDITOR environment variable, the core.editor configuration > variable, the VISUAL environment variable, or the EDITOR > environment variable (in that order). See git-var for details.

So to edit your message using nano, for example, you can run:

export GIT_EDITOR=nano
git commit

Solution 11 - Git

I use zsh on a Mac, and I can post multi-line commit messages within double quotes ("). Basically I keep typing and pressing return for new lines, but the message isn't sent to Git until I close the quotes and return.

Solution 12 - Git

If you are using Bash, hit C-x C-e (Ctrl+x Ctrl+e), and it will open the current command in your preferred editor.

You can change the preferred editor by tweaking VISUAL and EDITOR.

That's what I have in my .bashrc:

export ALTERNATE_EDITOR=''
export EDITOR='emacsclient -t'
export VISUAL='emacsclient -c'
export SUDO_EDITOR='emacsclient -t'

Solution 13 - Git

Here is a list of failing solutions on Windows with standard cmd.exe shell (to save you some trial-and-error time!):

  • git commit -m 'Hello Enter doesn't work: it won't ask for a new line

  • git commit -m "Hello Enter idem

  • git commit -m "Hello^ Enter idem

  • git commit -m 'Hello^ Enter World' looks like working because it asks "More?" and allows to write a new line, but finally when doing git log you will see that it's still a one-line message...

TL;DR: Even if on Windows, commandline parsing works differently, and ^ allows multiline input, it doesn't help here.

Finally git commit -e is probably the best option.

Solution 14 - Git

Personally, I find it easiest to modify commit messages after the fact in vi (or whatever your git editor of choice is) rather than on the command line, by doing git commit --amend right after git commit.

Solution 15 - Git

I don't see anyone mentioning that if you don't provide a message it will open nano for you (at least in Linux) where you can write multiple lines...

Only this is needed:

git commit

Solution 16 - Git

Sadly, git doesn't seem to allow for any newline character in its message. There are various reasonable solutions already above, but when scripting, those are annoying. Here documents also work, but may also a bit too annoying to deal with (think yaml files)

Here is what I did:

git commit \
    --message "Subject" \
    --message "First line$(echo)Second line$(echo)Third Line"

While this is also still ugly, it allows for 'one-liners' which may be useful still. As usually the strings are variables or combined with variables, the uglynes may be kept to a minimum.

Solution 17 - Git

I've just been looking for any answer to that question because many times, I used to see regexes for checking commit messages. Anyway, no idea worked during commit creating with -m flag.

So, the easiest way to achieve a multiline commit message is by typing only git commit into the terminal.
Then, you'll get an opportunity to input your desired note: input_commit_message

No more complicated way is needed.

Solution 18 - Git

IMO the initial commit message line is supposed to be to short, to the point instead of paragraph. So using git commit -m "<short_message>" will suffice

After that in order to expand upon the initial commit message we can use

git commit --amend

which will open the vim and then we can enter the explanation for the commit message which in my opinion easier than command line.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlan WhitelawView Question on Stackoverflow
Solution 1 - GitSimon RichterView Answer on Stackoverflow
Solution 2 - GitSimonView Answer on Stackoverflow
Solution 3 - GitesseView Answer on Stackoverflow
Solution 4 - GitJon CrowellView Answer on Stackoverflow
Solution 5 - GitDennis WilliamsonView Answer on Stackoverflow
Solution 6 - GitblonghoView Answer on Stackoverflow
Solution 7 - GitSaravanan MView Answer on Stackoverflow
Solution 8 - GitTobseView Answer on Stackoverflow
Solution 9 - GitPeter FarmerView Answer on Stackoverflow
Solution 10 - GitEugene YarmashView Answer on Stackoverflow
Solution 11 - GitAbizernView Answer on Stackoverflow
Solution 12 - GitAleks-Daniel Jakimenko-A.View Answer on Stackoverflow
Solution 13 - GitBasjView Answer on Stackoverflow
Solution 14 - GitamphibientView Answer on Stackoverflow
Solution 15 - GitRodrigo GraçaView Answer on Stackoverflow
Solution 16 - GitoliverView Answer on Stackoverflow
Solution 17 - GitmaciejwwwView Answer on Stackoverflow
Solution 18 - GitYug SinghView Answer on Stackoverflow