Git Commit Message Command Explained (git commit -m Examples)

Git Commit Message Command Explained (git commit -m Examples)

Git commit messages describe the purpose of changes made in a repository and help developers understand project history over time. Using the git commit command with the -m option allows you to add a commit message directly from the command line without opening the default editor.

In this guide, you will learn how to write Git commit messages, use the git commit -m command, create multi-line commit messages, and follow recommended practices with practical examples.


Git Commit Message Quick Cheat Sheet

CommandDescription
git commit -m "message"Commit staged changes with a message directly from the command line.
git commitOpen the default editor to write a commit message manually.
git commit -am "message"Commit all modified tracked files with a message in one command.
git commit --message="message"Alternative way to specify commit message using the long option.
git commit -m "title" -m "description"Create a multi-line commit message with title and body.
git commit --amend -m "new message"Modify the last commit message.
git commit --amendOpen editor to edit the last commit message.
git commit --allow-empty -m "message"Create a commit with a message even if no changes exist.
git logView full commit history including commit messages.
git log --onelineDisplay commit messages in a short single-line format.
git showDisplay detailed information about the latest commit including message.
git commit --verboseShow changes in the editor while writing the commit message.
git commit --no-editReuse the previous commit message when amending commits.
git commit --signoff -m "message"Add a Signed-off-by line to the commit message.
git commit --fixup <commit>Create a fixup commit message used during interactive rebase.
git commit --squash <commit>Create a squash commit message for combining commits later.
git commit --template=<file>Use a predefined template for commit messages.
git log --grep="keyword"Search commit history for specific commit messages.

Add a Commit Message from the Command Line with git commit -m

The most common way to add a commit message in Git is by using the -m option with the git commit command. This allows you to write the commit message directly in the terminal without opening the default editor.

First stage the changes using git add and then commit them with a message.

bash
git add file.txt
git commit -m "Add initial configuration file"

This records the staged changes along with the provided commit message.

Commit staged files with message

If multiple files are staged, the same command can be used to commit them together.

bash
git commit -m "Update authentication logic and validation rules"

Git will include all currently staged files in the commit.

Verify commit message using git log

You can confirm the commit message by checking the commit history and inspecting commits with git show.

bash
git log --oneline

This displays recent commits along with their messages.


Write a Commit Message Using Git Editor

If you run the commit command without the -m option, Git opens the default editor so you can write a more detailed commit message.

bash
git commit

Git opens the configured editor (such as Vim, Nano, or VS Code) where you can enter the commit message.

Write multi-line commit messages

Inside the editor, you can create a structured message with a subject line and body.

Example:

text
Add user authentication feature

Implemented login validation and password encryption.
Updated authentication middleware for improved security.

This structure improves readability of commit history, especially when reviewing changes using git log command.

After writing the message, save and close the editor to complete the commit.

Common editor commands:

  • Vim: :wq
  • Nano: CTRL + X then Y
  • VS Code: Save and close the editor window

Commit Changes with Message in One Command

Git also allows you to commit tracked file modifications directly without staging them individually.

Use git commit -am "message"

bash
git commit -am "Fix bug in login validation"

This command stages modified tracked files and commits them with the provided message.

The -a option is useful when working with already tracked files added earlier using git add.

  • You want to quickly commit modified files
  • You do not want to run git add separately
  • The files are already tracked by Git

Commit modified tracked files

Note that this command only works for files that Git is already tracking.

If new files exist, they must be staged manually.

bash
git add newfile.js
git commit -m "Add new API helper"

Edit the Last Commit Message

Sometimes the last commit message may contain a typo or incomplete description. Instead of creating another commit, you can update the message using git commit --amend.

Use git commit --amend -m

bash
git commit --amend -m "Fix login validation bug"

This replaces the previous commit message with the new one (see change commit message for older commits).

Amending is commonly used to:

  • Correct spelling mistakes
  • Improve commit descriptions
  • Clarify the purpose of the change

After amending the commit, verify the update:

bash
git log --oneline

The output will display the updated commit message in the commit history.


Write Multi-Line Commit Messages

Sometimes a single-line commit message is not enough to explain complex changes. Git allows you to write multi-line commit messages that include a subject line and a detailed body describing the change.

Subject and body structure

A typical multi-line commit message contains:

  • Subject line – short summary of the change
  • Blank line – separates subject and body
  • Body – detailed explanation of the change

This format makes commit history easier to read and understand.

Add detailed explanation in commit body

Run the commit command without the -m option so Git opens the default editor.

bash
git commit

You can then write a structured commit message in the editor.

Example multi-line commit message

Example of a well-structured commit message:

text
Add user authentication feature

Implemented login validation and password hashing.
Updated authentication middleware to handle token expiration.
Improved error handling for invalid login attempts.

The subject briefly describes the change, while the body explains the details.


Add Commit Message When Using Git Bash

Write commit message in terminal

You can write a commit message directly from the terminal using the -m option.

bash
git commit -m "Add input validation to login form"

This command commits the staged changes with the provided message.

Handling commit message prompts

If you run the commit command without the -m option:

bash
git commit

Git Bash opens the configured editor where you can write a detailed commit message.

Avoid empty commit message errors

Git prevents commits without a message. If you exit the editor without writing a message, Git shows an error such as:

text
Aborting commit due to empty commit message

To avoid this error, always provide a meaningful commit message before saving.


Git Commit Message Examples

Simple commit message example

bash
git commit -m "Add configuration file"

This is suitable for small personal projects.

Feature commit example

bash
git commit -m "Add user profile page"

Used when introducing a new feature to the project.

Bug fix commit example

bash
git commit -m "Fix login validation bug"

Describes a bug fix in the application.

Documentation commit example

bash
git commit -m "Update installation guide"

Used when modifying documentation.


Subject line best practices

A good subject line should:

  • Be short and descriptive
  • Use imperative tone (for example: Add feature, Fix bug)
  • Stay within about 50 characters

Example:

text
Fix authentication token validation

Body and footer structure

If additional details are required, add a body and optional footer.

text
Fix authentication token validation

Updated token verification logic.
Added error handling for expired tokens.

Reviewed-by: John Doe

Writing descriptive commit messages

A good commit message should explain:

  • What change was made
  • Why the change was needed
  • Any important context for reviewers

Clear messages make it easier for developers to understand the project history.


Conventional Commit Message Format

Structure of conventional commits

The general format is:

text
<type>[optional scope]: <description>

Optional body and footer can be added for more details.

Common commit types (feat, fix, docs, refactor)

Some common commit types include:

  • feat – introduce a new feature
  • fix – fix a bug
  • docs – update documentation
  • refactor – restructure code without changing behavior
  • test – add or update tests
  • chore – maintenance tasks

Example conventional commit message

Example commit using conventional format:

text
feat(auth): add login validation middleware

Improves authentication security and error handling.

This format helps generate changelogs and maintain a structured commit history, especially when working with history rewriting using git rebase.


Frequently Asked Questions

1. How do I add a commit message in Git?

You can add a commit message in Git using git commit -m "message". The -m option allows you to write the commit message directly from the command line without opening the default editor.

2. How do I write a multi-line commit message in Git?

Run git commit without the -m option. Git will open the default editor where you can write a subject line, body, and footer for a detailed commit message.

3. What is the best format for a Git commit message?

A good commit message usually contains a short subject line followed by an optional body explaining the change. Many teams follow the conventional commit format <type>: <description> such as fix: resolve login bug.

4. How do I edit the last commit message?

You can modify the last commit message using git commit --amend -m "new message". This replaces the previous commit message with the updated one.

5. Why are commit messages important in Git?

Commit messages document the purpose of code changes and help developers understand project history. Clear commit messages improve collaboration, debugging, and release tracking.

Summary

Writing clear and meaningful Git commit messages is essential for maintaining a readable project history within a proper Git workflow. Using commands such as git commit -m or git commit allows developers to describe what changes were made and why they were necessary.

Following a consistent structure for commit messages helps teams collaborate more effectively, track changes easily, and understand the evolution of a codebase over time. Whether working on a personal project or a shared repository, well-written commit messages improve debugging, code reviews, and release management.


Official Documentation

For more details about writing and managing commit messages in Git, refer to the official documentation:

Steve Alila

Steve Alila

Specializes in web design, WordPress development, and data analysis, with proficiency in Python, JavaScript, and data extraction tools. Additionally, he excels in web API development, AI integration, and data presentation using Matplotlib and Plotly.