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
| Command | Description |
|---|---|
git commit -m "message" | Commit staged changes with a message directly from the command line. |
git commit | Open 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 --amend | Open 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 log | View full commit history including commit messages. |
git log --oneline | Display commit messages in a short single-line format. |
git show | Display detailed information about the latest commit including message. |
git commit --verbose | Show changes in the editor while writing the commit message. |
git commit --no-edit | Reuse 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.
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.
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.
git log --onelineThis 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.
git commitGit 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:
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 + XthenY - 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"
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 addseparately - 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.
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
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:
git log --onelineThe 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.
git commitYou can then write a structured commit message in the editor.
Example multi-line commit message
Example of a well-structured commit message:
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.
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:
git commitGit 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:
Aborting commit due to empty commit messageTo avoid this error, always provide a meaningful commit message before saving.
Git Commit Message Examples
Simple commit message example
git commit -m "Add configuration file"This is suitable for small personal projects.
Feature commit example
git commit -m "Add user profile page"Used when introducing a new feature to the project.
Bug fix commit example
git commit -m "Fix login validation bug"Describes a bug fix in the application.
Documentation commit example
git commit -m "Update installation guide"Used when modifying documentation.
Recommended Git Commit Message Format
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:
Fix authentication token validationBody and footer structure
If additional details are required, add a body and optional footer.
Fix authentication token validation
Updated token verification logic.
Added error handling for expired tokens.
Reviewed-by: John DoeWriting 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:
<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 featurefix– fix a bugdocs– update documentationrefactor– restructure code without changing behaviortest– add or update testschore– maintenance tasks
Example conventional commit message
Example commit using conventional format:
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 usinggit 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?
Rungit 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 usinggit 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:



![Git Error: Cannot Delete Branch Checked Out or Used by Worktree [SOLVED]](/cannot-delete-branch-checked-out-at/git-cannot-delete-branch_hu_3f86bd25a59d627d.webp)






