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
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"
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
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"
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
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")"
Next, you can:
$ git commit -m "$msg"
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
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"
Or use standard input instead of temporary files:
$ printf "Commit Title\nCommit Description" | git commit -F-
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!