90

When I use git log to check out my commit explanatory note I want it to look like this:

1. what I changed
2. blank line
3. why I changed it

...being in 3 lines not 1 like this:

1. what i changed  2. blank line 3. why i changed

However, git log shows it in one line. So, how do I get this to happen using git commit -m?

6

7 Answers 7

144

You just use the following command:

$ git commit -m "1. what i changed
> 2. blank line
> 3. why i changed"

In your terminal, just hit 'enter' for a new line. The commit message won't end until you add the closing quote. The git log will look like:

$ git log
commit abcde2f660c707br2d20411581c4183170c3p0c2
Author: Alex Pan <[email protected]>
Date:   Tue Apr 28 20:52:44 2015 -0700

    1. what i changed
    2. blank line
    3. why i changed
5
  • 2
    How do I fix this with interactive rebase? if I do the same procedure as with commit -m, I get an error that there is an unknown command on a line. I am trying to reword my commit message as it was "Test\nTest" instead of an actual newline character it literally read "\n".
    – AgentM
    Jun 27, 2018 at 21:44
  • 9
    Using a windows machine, when I hit enter, the commit message is truncated. It does not wait for a closing quote.
    – David Diaz
    May 4, 2020 at 20:46
  • 2
    This doesn't work on mac. At least for me. Feb 24, 2021 at 22:09
  • 2
    'shift + return' on mac does work Sep 8, 2022 at 10:02
  • Does that work inside a .sh?
    – Maf
    Mar 29, 2023 at 9:03
82

Use two --message/-m options, first one for the subject and second one for the body.

Excerpt from documentation:

-m <msg>

--message=<msg>

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

In your case it does exactly what you want, inserts a blank line between first and second lines.

git commit -m "Subject: what I changed" -m "Body: why I changed it"

This is useful if you want to amend previously added comment.

3
  • 9
    +1 Just wanted to mention that in github the first comment is shown as the main commit message and the second one is shown as the description (with smaller font).
    – MLow
    Aug 2, 2019 at 15:31
  • 2
    For some reason, my console didn't allow me to press enter so I went with this solution instead.
    – Semmel
    Jan 18, 2021 at 11:12
  • 1
    Very straightforward. Thanks. That's also very useful to be used in scripts to add commits with some rules of your company. In mine we need to add commits with multiple lines following a specific pattern and that's a very easy way to do that.
    – Maf
    Mar 29, 2023 at 14:10
22

The multiple-line format you describe is the recommended one with Git (See DISCUSSION in the documentation of git commit). The simplest way to do it is to use git commit without -m, and write your message in your text editor.

19

I find it much easier to save the commit message to a file, and then use the -F option.

Example:

$ cat > /tmp/commit_msg.txt
DE123 bug fix: incorrect fetching of instance details
- fixed this and that
- also did such and such
$ git commit -F /tmp/commit_msg.txt

You could also use an editor to edit the message file before the commit.

9

Rather than use a temp file when trying to do this programmatically you can use stdin

git commit -F-

then write the message to stdin

8
  • Using a file makes it easier to prepare the text with an editor. I was just using cat as a way to keep it all visible in the CLI context...
    – yaronyogev
    Jun 1, 2016 at 10:07
  • 5
    Example of this if anyone is wondering: echo "$my_multiline_var" | git commit -F- Oct 3, 2019 at 22:07
  • @tony-brix anyway to do this for both a subject and body? Feb 12, 2022 at 17:32
  • @onlinespending I believe the subject is just the first line of the commit and the body is the rest. You may need a blank line between the subject and body on some platforms.
    – Tony Brix
    Feb 15, 2022 at 4:45
  • @tony-brix no way to provide two -F- options. that's what I was after. I had to go the route of storing the two messages as environment variables and using "bash -c" from my python script Feb 15, 2022 at 16:43
5

I needed to have a bash script do multi-line git commits for me, so here are two options I came up with:

  1. Write to a temporary file then commit with the contents of the file as the message:

     printf "first line\nsecond line\nthird line" > "file.txt"
     git commit -F "file.txt"
    
  2. (My preferred approach): Write to a temporary variable then commit with the contents of the variable as the message. Note that the quotes around $MSG when doing any command to recall the contents of the variable are required! Without them, you'll lose your newlines.

     MSG="$(printf "first line\nsecond line\nthird line")"
     git commit -m "$MSG"
    

As an extension of this 2nd approach, in case you need to script building the message in multiple pieces or steps, that is possible too. WATCH OUT though! Notice where I place my newline (\n) characters. I do NOT place them at the end of any printf string. That's because if I do they will get gobbled up, because bash automatically removes any trailing newline characters, since it's dumb like that. So, do it like this instead, which works just fine:

    MSG="$(printf "first line")"
    MSG="$(printf "${MSG}\nsecond line")"
    MSG="$(printf "${MSG}\nthird line")"
    git commit -m "$MSG"

Sample git log output from any of the above git commits:

commit e1983659c6ae2e9d2eb4332657329837582fc32b (HEAD -> master)
Author: Gabriel Staples <[email protected]>
Date:   Tue Mar 24 00:55:31 2020 -0700

    first line
    second line
    third line

References:

  1. Unix & Linux: "Why does shell Command Substitution gobble up a trailing newline char?"
  2. VERY USEFUL! ==> How can I have a newline in a string in sh? <==
4
  • 1
    Note that your printfs all suffer from a problem: if the strings contain a % character, printf may want to read an argument. So for arbitrary text you should always use something like printf '%s\n' "text foo" "text bar" "baz".
    – Jens
    Mar 24, 2020 at 11:28
  • I see. I would have expected your format string to require three usages of %s, like would be required in C: printf '%s\n%s\n%s\n' "text foo" "text bar" "baz". Why does your format string with a single %s still work? It must be a bash thing. Mar 24, 2020 at 17:03
  • 1
    No, it's a printf thing. POSIX specifies that the format is reused for any remaining argument.
    – Jens
    Mar 25, 2020 at 8:16
  • 1
    Or do: MSG+="$(printf "\nsecond line")"
    – yzernik
    Oct 7, 2021 at 3:52
3

When writing a commit message just hit Shift + Enter when you want to go to the next line. It looks like this:

git commit -m "multiline
dquote> commit 
dquote> 
dquote> 1"

And the result will be:

multiline
commit
    
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.