How can I make git commit messages divide into multiple lines?

GitCommit

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

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

Excerpt from documentation:

> -m >
> --message= > > 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.

Solution 3 - Git

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.

Solution 4 - Git

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.

Solution 5 - Git

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

Solution 6 - Git

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! ==> https://stackoverflow.com/questions/3005963/how-can-i-have-a-newline-in-a-string-in-sh/10618140#10618140 <==

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
QuestionfernandoView Question on Stackoverflow
Solution 1 - GitAlex PanView Answer on Stackoverflow
Solution 2 - GitolegtaranenkoView Answer on Stackoverflow
Solution 3 - GitMatthieu MoyView Answer on Stackoverflow
Solution 4 - GityaronyogevView Answer on Stackoverflow
Solution 5 - GitTony BrixView Answer on Stackoverflow
Solution 6 - GitGabriel StaplesView Answer on Stackoverflow