86

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?

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
fernando
  • 949
  • 1
  • 7
  • 13

7 Answers7

138

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 <alexpan@stackoverflow.com>
Date:   Tue Apr 28 20:52:44 2015 -0700

    1. what i changed
    2. blank line
    3. why i changed
Alex Pan
  • 4,341
  • 8
  • 34
  • 45
79

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.

Elijah Lynn
  • 12,272
  • 10
  • 61
  • 91
olegtaranenko
  • 3,722
  • 3
  • 26
  • 33
  • 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). – Hamed2005 Aug 02 '19 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 '21 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 '23 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.

Matthieu Moy
  • 15,151
  • 5
  • 38
  • 65
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.

yaronyogev
  • 428
  • 3
  • 10
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

Tony Brix
  • 4,085
  • 7
  • 41
  • 53
  • 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 01 '16 at 10:07
  • 5
    Example of this if anyone is wondering: `echo "$my_multiline_var" | git commit -F-` – Chris Deacy Oct 03 '19 at 22:07
  • @tony-brix anyway to do this for both a subject and body? – onlinespending Feb 12 '22 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 '22 at 04: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 – onlinespending Feb 15 '22 at 16:43
  • @onlinespending I don't believe git has the concept of subject and body. There is only one commit message per commit. You will have to concatenate the message correctly according to the platform that will display the commit message subject and body. – Tony Brix Feb 15 '22 at 20:14
  • that's not correct. you can provide as many -m messages as you'd like. I was simply after a way to provide -F multiple times like -m since that would allow a programmatic way to create multiple multi-line messages from a python script. But unfortunately git only allows one -F option. but anyways, like I said using environment variables was an acceptable workaround – onlinespending Feb 15 '22 at 23:10
  • Multiple -m doesn't create multiple commit messages it concatenates them into one commit message since commits can only have one message. https://git-scm.com/docs/git-commit#Documentation/git-commit.txt--mltmsggt – Tony Brix Feb 16 '22 at 04:55
4

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@gmail.com>
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? <==
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
  • 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 '20 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. – Gabriel Staples Mar 24 '20 at 17:03
  • 1
    No, it's a printf thing. POSIX specifies that the format is reused for any remaining argument. – Jens Mar 25 '20 at 08:16
  • 1
    Or do: `MSG+="$(printf "\nsecond line")"` – yzernik Oct 07 '21 at 03:52
1

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
Alex Gongadze
  • 191
  • 1
  • 5