DEV Community

Cover image for How to Commit Multiline Messages in git commit
Zachary Lee
Zachary Lee

Posted on • Originally published at webdeveloper.beehiiv.com on

How to Commit Multiline Messages in git commit

As developers, when using Git to commit code to a remote repository, we need to write information about this modification. On the command line, we use the git commit command, such as git commit -m which allows you to add a line of information. But sometimes a multi-line message with a title and a specific description may be more indicative of your intent, such as the following:

Commit Title: Briefly describe what I changed
Commit Description: Detailed instructions for changing it
Enter fullscreen mode Exit fullscreen mode

So how to achieve this?


1. Use a text editor

Use git commit without the -m or git commit -v, which will take you to a text editor. So then you can add multiple lines of text using your favorite text editor.

2. Multiple -m options

If you don’t want to see wordy diffs, you can use multiple -m options. Just like this:

$ git commit -m "Commit Title" -m "Commit Description"
Enter fullscreen mode Exit fullscreen mode

This is because if multiple -m options are given, their values will be concatenated into separate paragraphs, which can be found in the git documentation.

Next git log will look like this:

$ git log
commit 1e8ec2c4e820fbf8045b1c7af9f1f4f23262f755
Author: Your Name you@example.com
Date: Sat Sep 24 20:18:15 2022 -0700

Commit Title

Commit Description
Enter fullscreen mode Exit fullscreen mode

3. Open quotes, press Enter

Another easier way is to type git commit -m " and hit Enter to enter the multiline, and use closing quotes when closing. This looks like this:

$ git commit -m "
> Commit Title
> Commit Description"
Enter fullscreen mode Exit fullscreen mode

Next git log will look like this:

$ git log
commit 7d75a73e41b578a1e2130372a88a20ed1a0a81e4
Author: Your Name you@example.com
Date: Sat Sep 24 20:22:02 2022 -0700

Commit Title
    Commit Description
Enter fullscreen mode Exit fullscreen mode

4. Shell Environment Variables

Don’t forget that you can define environment variables in the shell, for example, you can define temporary environment variables with newlines:

$ msg="
> Commit Title
> Commit Description"

# or
$ msg="$(printf "Commit Title\nCommit Description")"
Enter fullscreen mode Exit fullscreen mode

Next, you can:

$ git commit -m "$msg"
Enter fullscreen mode Exit fullscreen mode

That’s it, git log will output:

$ git log
commit 056e35c37d199c0f3904e47d2107140267608c4a
Author: Your Name you@example.com
Date: Sat Sep 24 20:42:11 2022 -0700

Commit Title
    Commit Description
Enter fullscreen mode Exit fullscreen mode

5. Use the -F option

Introduction from the documentation:

-F

 — file=
Take the commit message from the given file. Use - to read the message from the standard input.

So you can write a multi-line message in a temporary file before committing. Like the following:

$ printf "Commit Title\nCommit Description" > "temp.txt"
$ git commit -F "temp.txt"
Enter fullscreen mode Exit fullscreen mode

Or use standard input instead of temporary files:

$ printf "Commit Title\nCommit Description" | git commit -F-
Enter fullscreen mode Exit fullscreen mode

Conclusion

Here are a few methods I saw, you can choose one of them according to your preference. If you have other ways, feel free to share.

If you find this helpful, please consider subscribing to my newsletter for more insights on web development. Thank you for reading!

Top comments (2)

Collapse
 
cerico profile image
cerico • Edited

You can extend these by making a shell alias or aliases, that auto-prepends semantic versioning (fix, feat, feat!)

I use

fix () {
    echo "#fix: $1" > ~/.config/git/commit-msg-template
    git commit
}
Enter fullscreen mode Exit fullscreen mode
minor () {
    echo "#feat: $1" > ~/.config/git/commit-msg-template
    git commit
}
Enter fullscreen mode Exit fullscreen mode

though you could alse combine into one function too. Then you can run as eg

minor "increased client_max_body_size in nginx"
Enter fullscreen mode Exit fullscreen mode

This will open up the text editor, whereupon you delete the # so that the message has changed, and either save if you want a one line commit, or add the rest of the multiline commit before saving. It also means all your commits are prefixed by default

Collapse
 
capte profile image
Capte

Cool