How Can I Make Git Commit Messages Divide into Multiple Lines?

How Can I Make Git Commit Messages Divide into Multiple Lines?

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

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

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

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.

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.

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

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

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
Marcus Vance
Author

Marcus Vance

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.