DevOps

Git commit: create a title and description from CLI

Create a Git commit with a separate title and body from the CLI. Use multiple -m flags, -F file input, or commit templates for consistent commit messages.

1 answer 1 view

How can I create a Git commit from the command line that includes both a commit title and a separate commit description (body), similar to the GitHub web interface? When I commit locally, git opens my default editor and I write a message that GitHub then splits into a title and body. Is there a way to provide a nicely formatted title and description directly from the terminal (for example using git commit -m, multiple -m flags, an editor template, or other command-line options)?

You can provide both a commit title (summary) and a separate commit description (body) straight from the terminal: use multiple -m flags (for example git commit -m "Title" -m "Body..."), read a prepared message file with -F, or prefill the editor with a commit template (commit.template or -t). Git treats the text before the first blank line as the commit title and the rest as the body — the same split that GitHub shows.


Contents


Quick answer: git commit with a title and body

Short version: yes. The CLI supports several ways to give a nicely formatted commit title and a separate description without manually typing everything into your editor. The easiest options are:

  • Use multiple -m flags: git commit -m "Short summary" -m "Longer description...". Each -m becomes a paragraph; the first paragraph is treated as the title. See the official manual for the option details: Git - git-commit Documentation.
  • Use -F <file> to read the whole message (title on the first line, blank line, then body) from a file.
  • Use a commit template (git config commit.template <file> or git commit -t <file>) so git commit opens your editor pre-filled with a structured message.

All hosting providers and tools (including GitHub) display the first paragraph as the summary/title, so these CLI techniques produce the same split you see on the web.


Use multiple -m flags with git commit

Syntax and behavior:

  • -m <msg> sets a commit message. If you pass multiple -m options, Git concatenates them as separate paragraphs (it inserts a blank line between them). The first paragraph becomes the commit title/summary. See the manual: https://git-scm.com/docs/git-commit.

Example:

bash
git add file.txt
git commit -m "Add login endpoint" -m "Validate inputs and return JSON; add unit tests."

Resulting commit message (what git stores and what GitHub shows):

Add login endpoint

Validate inputs and return JSON; add unit tests.

Shell tips and alternatives:

  • On shells that support ANSI-C quoting (bash), you can include literal newlines inside a single -m with $'...':
bash
git commit -m $'Short summary\n\nLonger description line 1\nLine 2'

That relies on your shell to produce actual newline characters; the -m flag then receives a multi-line string.

  • If the body is long or maintained in a file, prefer -m + -m or -F rather than embedding complex quoting in a one-liner.

For a short guide confirming multiple -m usage, see the phoenixNAP tutorial: https://phoenixnap.com/kb/git-commit.


Use a file with -F to provide title + body

When you already have the message written (or want to script it), create a file where the first line is the title, a blank line separates the body, then the body text follows.

Example (here-doc to create a temp message file):

bash
cat > /tmp/commit-msg.txt <<'EOF'
Fix crash when user ID is nil

Guard against nil user IDs in the session handler. This avoids a
runtime exception when a request arrives with a missing id field.
Added unit test and an integration test case.
EOF

git add .
git commit -F /tmp/commit-msg.txt

Notes:

  • The file format is plain text: first paragraph → summary, remaining paragraphs → body.
  • Lines that start with # are commonly used as comments in templates/editors and are ignored by Git when editing messages; templates and editor workflows often show commented guidance lines. See community examples on templates and multiline messages: https://gitscripts.com/git-commit-multiline-message.

Commit templates and commit.template

If you want a consistent structure for every commit (prefixes, checklists, placeholders), use a commit template:

  1. Create a template file, e.g. ~/.gitmessage:
Short summary (50 chars or less)

- Why:
- What:
- How:
  1. Configure Git to use it:
bash
git config --global commit.template ~/.gitmessage
  1. Run git commit as usual. Your editor opens pre-filled with the template; fill in the lines and save.

You can also call a specific template for a single commit with git commit -t /path/to/template. Templates are handy when teams want consistent fields (issue number, changelog blurb, etc.). See an example write-up on global commit templates: https://www.stefanjudis.com/today-i-learned/global-git-commit-templates/ and a community guide: https://dev.to/albz/level-up-your-git-commits-with-custom-templates-5786.


Edit after -m, amend, and cleanup options

Want to prefill a message but still tweak it in your editor? Use -e (or --edit) together with -m or -F:

bash
git commit -m "Draft title" -e

That opens the editor with the provided message preloaded so you can refine the body.

To change the most recent commit message (for local edits before pushing), use --amend:

bash
git commit --amend -m "New title" -m "New body..."
# or
git commit --amend -F /tmp/new-message.txt

Cleanup: Git can clean up and remove comments/whitespace lines from messages; the --cleanup option controls this behavior (see git commit --help).


Examples and best practices for commit messages

Quick examples:

  • Minimal, no editor:
bash
git commit -m "Fix typo in README" -m "Corrected the installation example and added missing flag note."
  • From a file:
bash
git commit -F ./commit-message.txt
  • Template + commit:
bash
git config --global commit.template ~/.gitmessage
git commit

Best-practice format (widely used):

  • Title: one short line, ~50 characters, imperative mood (e.g., “Add user login”).
  • Blank line.
  • Body: explain the why and how; wrap at ~72 characters.

Why follow this? Tools and hosting sites expect the first paragraph to be the summary and show it in lists and PR titles. Clean, consistent messages make history easier to scan and automate.

Want to avoid opening the editor every time? Use -m twice or prepare a message file and use -F. For scripting, git commit -m "Title" -m "Body..." is simple and portable. For team consistency, use a commit.template.


Sources


Conclusion

You can create a Git commit with both a title and a separate description directly from the CLI using git commit -m "Title" -m "Body...", git commit -F <file> (file with a blank line between title and body), or by using commit templates (commit.template or -t) to prefill the editor. Use -e to edit after providing a message and --amend to rewrite the last commit. Follow the conventional format (short summary, blank line, wrapped body) so your commit message displays cleanly in GitHub and other tools.

Authors
Verified by moderation
Moderation
Git commit: create a title and description from CLI