842

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?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Alan Whitelaw
  • 14,051
  • 9
  • 32
  • 50
  • 8
    As a note, here's a link which summarizes good commit message conventions - https://github.com/erlang/otp/wiki/Writing-good-commit-messages if it helps someone. – WeirdElfB0y Jan 07 '16 at 06:25
  • 8
    Something like `GIT_EDITOR="emacs" git commit -m 'paragraph1' -m 'paragraph2' -e` would help you avoid `vim`. – jotik May 05 '16 at 07:42

17 Answers17

789

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
Sandra
  • 349
  • 5
  • 16
Simon Richter
  • 26,160
  • 1
  • 38
  • 59
  • 59
    @Peter Farmer's answer later on mentions that Git convention is apparently something like: 1 line for summary, two line breaks, then a detailed message. – Nick Spacek Oct 14 '11 at 15:30
  • 5
    Also, see below post by @esse. A simple carriage return does the trick. – Hakan Ensari Jul 05 '12 at 09:42
  • 1
    Precisely, see **[this post by @esse](http://stackoverflow.com/questions/5064563/add-line-break-to-git-commit-m-from-command-line/9305137#9305137)**. – imy Apr 13 '14 at 20:23
  • "here document" is very handy in other context where one does not want to (cannot) create temporary files. – Vladislavs Dovgalecs Jan 31 '15 at 00:34
  • 8
    @MohamadAli, on Windows, commandline parsing works [differently](http://stackoverflow.com/questions/605686) – Simon Richter Nov 03 '15 at 19:43
  • I landed here trying to understand how to *amend* a commit message without hitting the editor. My recipe: – qneill Dec 03 '15 at 23:52
  • 2
    @KelvinShadewing, yes, but with the difference that shell substitution rules apply to the message, so you need to escape dollar signs and other metacharacters. On the other hand it allows you to use variables. – Simon Richter Jul 16 '17 at 17:06
  • 2
    @SimonRichter On Windows commandline parsing works differently, but still, using `git commit -m "Hello^` `World"` doesn't work. The input will be given in 2 lines, but when doing `git log`, you will see only one line. – Basj Jun 29 '18 at 10:17
  • @SimonRichter What does `-` after `-F` signify? – Porcupine Sep 20 '18 at 20:57
  • 3
    @Nikhil, many programs support a single dash as a file name to mean stdin or stdout. With the *here document*, the `git` command can read the message text from stdin, and the `-F` option gives the file name to read the message from. – Simon Richter Sep 20 '18 at 23:22
  • Just as a note: The first method also works with PowerShell (tested with version 5.1, Windows 10, Desktop) - with both, single and double quotes. When in doubt you can test this behavior with `echo "line a` [return] `line b"` [return] before you use it in a `git commit`. – Andreas Linnert Oct 11 '18 at 10:10
  • @SimonRichter, so `-F -` with a space in between also works, right? – Enlico Jan 28 '20 at 21:37
619

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.
Simon
  • 27,089
  • 8
  • 73
  • 87
419

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
esse
  • 5,183
  • 3
  • 14
  • 20
  • 8
    [Abizern's answer](http://stackoverflow.com/a/5064694/173497) clarified for me why this works – the Bash shell interprets a Enter key-press as a new line until the first double-quote character is 'closed' (with a subsequent double-quote character). – Kenny Evitt Jul 18 '13 at 19:42
  • 1
    I have to agree that this is a much more effective, easier and practical solution that the accepted answer. It works well for me using Git 1.8.2.1. +1 from me. – crmpicco Aug 22 '13 at 10:35
  • 2
    This isn't a special function of the *Enter* key, but rather related to the quotes. Whether you use double or single quotes doesn't really matter, except for variable expansion and escaping special characters -- that's why I chose single quotes in my answer. – Simon Richter Dec 20 '14 at 23:17
  • 2
    Don't use this in zsh! Terminal will close and you lose what you typed. – laike9m Jun 16 '15 at 05:13
  • 4
    Works in Gitbash for Windows. – Omar Tariq Nov 06 '16 at 16:17
  • How to go to the first line of commit. – viper Dec 12 '16 at 12:04
  • How to go back to the previous line? – skytree Nov 18 '17 at 22:22
  • Works with `zsh` for me! – ernestkamara Aug 29 '18 at 07:22
  • So `git` is behaving correctly -- it's just waiting for me to end the string. Makes sense. The only thing I notice, is that once I press `Enter` and I'm on the next line, I can't go back to the line above and edit. I must use `Ctrl+C` to erase the message and start over (_or copy msg, edit outside of git and paste back_). But this is still a great answer. Thanks! – Chris22 Sep 09 '19 at 22:20
  • Does not work with Windows CMD. It just commits when you hit ENTER key. – Tushar Jun 18 '20 at 15:21
  • How to get out of this? – Hyfy May 12 '21 at 16:24
127

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

Jon Crowell
  • 19,362
  • 12
  • 81
  • 104
  • 8
    Awesome answer. I've looked around for this for ages and tried numerous different ways to format my Git commit messages, but this works the best. I can confirm it works at the prompt with Git 1.8.2.1. – crmpicco Aug 22 '14 at 16:21
  • 1
    In Powershell you can do `n for line break – Ecropolis Sep 24 '15 at 15:38
  • 3
    It worked greatly for me in **Git Bash for Windows**. – Ulysses Alves Nov 13 '15 at 11:37
  • 1
    This *should* be the chosen answer as it is the most compatible method, this doesn't rely on any specific CLI prompt like some other suggestions do.. –  Apr 08 '16 at 09:03
  • 1
    This is a better answer and should be selected as the answer. Not only this goes in line as a default behaviour, and provides a much cleaner commit message but also it is somewhat more flexible with multiple -m. Also even though it looks windows specific and has attracted windows related comments, it works fine in Linux as well. – 0xc0de Apr 09 '18 at 08:23
  • I'll try this one. – Chris22 Sep 09 '19 at 22:15
  • Pressing enter submitted the commit instead of starting a new line... – Sam Sverko Apr 14 '21 at 20:32
  • @SamSverko, make sure you have a -m for both title and comment on the first line, and only open quotes for the comment on the first line. I added another screenshot that may be a little clearer. – Jon Crowell Apr 14 '21 at 23:46
  • plus one for including a verification with the git log -1 – DRP May 04 '21 at 19:59
124

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.

Dennis Williamson
  • 303,596
  • 86
  • 357
  • 418
  • @rsy: What version of Bash are you using? What do you see when you do `echo $'one\ntwo'` ? – Dennis Williamson Jun 02 '15 at 20:43
  • 1
    rsy$ bash --version GNU bash, version 3.2.53(1)-release (x86_64-apple-darwin13) Copyright (C) 2007 Free Software Foundation, Inc. The output of that command is, as expected, shown in two different lines! – ccoutinho Jun 02 '15 at 21:49
  • for me on windows 7 it`s best option Thank you – Mohamad Ali Nov 03 '15 at 13:37
  • 2
    The $ is key here and I didn't notice it at first glace. Otherwise I just get a \n in the middle of my message. – ChrisBob Mar 30 '17 at 19:19
  • @ChrisBob: Thanks for pointing out how easy that is to miss. I updated my answer to explain it and include a link to the documentation. – Dennis Williamson Mar 30 '17 at 19:46
  • 1
    You don't even have to use the $'...' for the whole string; using this just around the newline character will work: `git commit -m "first line"$'\n'"second line"`. Just note you have to close the previous string before starting your `$'string'`. – PlasmaBinturong Jan 30 '19 at 17:18
  • @PlasmaBinturong: That's true, but to me it looks a lot messier and would be easier to miss making sure all the extra quotes match up. – Dennis Williamson Jan 30 '19 at 18:29
  • Note that this doesn't work with GPG signing enabled. – Paul Razvan Berg Jun 03 '20 at 21:04
  • How can I do that if I now have put `MSG="first line\nsecond line"` to a varible first? `… -m $'$MSG'` will not work. – white_gecko Nov 09 '20 at 10:22
47

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"
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Saravanan M
  • 4,487
  • 4
  • 31
  • 37
47

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"
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Peter Farmer
  • 7,672
  • 5
  • 24
  • 29
44

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.

Tobse
  • 598
  • 4
  • 8
36

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.

blongho
  • 573
  • 5
  • 9
25

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
Eugene Yarmash
  • 119,667
  • 33
  • 277
  • 336
21

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Abizern
  • 129,329
  • 36
  • 198
  • 252
7

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'
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
  • 1
    So why did somebody downvote this? This is the most convenient way to work with multiline commands in bash, you just have to configure it right once. I've been using other stupid suggestions shown in other answers here, but once you learn to edit your commands in your favorite text editor there is just no way back. – Aleks-Daniel Jakimenko-A. Apr 18 '17 at 00:06
  • 2
    This is so heartbreakingly useful I just can't believe I lived my past years without this. – SoonDead Mar 12 '18 at 15:10
6

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.

Basj
  • 29,668
  • 65
  • 241
  • 451
5

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.

amphibient
  • 25,192
  • 44
  • 130
  • 215
3

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
Rodrigo Graça
  • 1,477
  • 3
  • 14
  • 23
2

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.

oliver
  • 541
  • 4
  • 4
-1

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.

Yug Singh
  • 2,571
  • 3
  • 17
  • 42