A Detailed Guide to Git Commits and Commit Message Guidelines

Committing is a core Git operation; each commit creates a unique version snapshot that records the complete state of the project at a given point in time.

1. Submitting Basic Concepts

(1) What Is a Commit?

A commit is the basic unit of Git version control and contains the following information:

100%
graph TB
    A[Workspace<br/>Working Directory] -->|git add| B[Buffer<br/>Staging Area]
    B -->|git commit| C[Repository<br/>Repository]
    C --> D[Generate a commit object<br/>Commit Object]
    D --> E[SHA-1Hash<br/>a1b2c3d4e5f6...]
    
    style A fill:#fff3cd
    style B fill:#d4edda
    style C fill:#c3e6cb
    style E fill:#cce5ff

(2) Submission Process

The complete submission process consists of three steps:

  1. Modify File: Edit a file in the workspace
  2. Stage Changes: Use git add to add changes to the staging area
  3. Create a commit: Use git commit to commit the contents of the staging area to the repository

(3) The Purpose of Submission


2. Basic Submission Operations

(1) First Submission

▶ Example: Creating Your First Commit

BASH
# Initialize the repository
git init

# Create a File
echo "# My Project" > README.md

# View Status
git status
# Untracked files:
#   README.md

# Add to Stash
git add README.md

# Submit
git commit -m "Initial commit"

# Output:
# [main (root-commit) a1b2c3d] Initial commit
#  1 file changed, 1 insertion(+)
#  create mode 100644 README.md

(2) Regular Submissions

▶ Example: Standard Submission Process

BASH
# Edit File
echo "## Features" >> README.md

# View and Edit
git diff
# +## Features

# Add to Stash
git add README.md

# View Staging Status
git status
# Changes to be committed:
#   modified:   README.md

# Submit
git commit -m "Add features section"

# View commit history
git log --oneline
# a1b2c3d Add features section
# b2c3d4e Initial commit

(3) Bulk Submission

▶ Example: Batch Addition and Submission

BASH
# Create Multiple Files
touch file1.js file2.js file3.js

# Add All Files
git add .

# Or add specific types of files
git add *.js

# Submit
git commit -m "Add multiple JavaScript files"

# View Submission
git show
# commit a1b2c3d4e5f6...
# Author: Zhang San <zhangsan@example.com>
# Date:   Mon Jan 1 10:00:00 2026 +0800
#
#     Add multiple JavaScript files
#
# diff --git a/file1.js b/file1.js
# new file mode 100644
# index 0000000..e69de29

3. Submission Guidelines

(1) The Importance of Submitting Information

Good commit messages can:

(2) Conventional Commits Standard

This is currently the most popular commit message convention, and the format is as follows:

TEXT
<type>(<scope>): <subject>

<body>

<footer>

Explanation of Each Section:

(3) Submission Type

Type Description Use Case
feat New Feature Add a new feature
fix Bug Fix Fix Program Error
docs Document Edits Update Document Content
style Code formatting Formatting changes that do not affect the meaning of the code
refactor Refactoring Code refactoring that neither adds features nor fixes bugs
perf Performance Optimization Improving Code Performance
test Test Add or modify test code
chore Build/Tools Changes to the build process or supporting tools
ci CI Configuration Modify the CI configuration file
revert Revert Commit before the revert

▶ Example: Standard Submission Information

BASH
# Single-line commit - New Features
git commit -m "feat: Add User Login Functionality"

# Single-line commit - BugFix
git commit -m "fix: Fix the login authentication error"

# Range-Based Submissions
git commit -m "feat(auth): AddJWTCertification Support"

# Multi-line commit
git commit -m "feat(user): Add a user registration feature" -m "Supports registration via email and phone number" -m "Add Form Validation and Error Messages"

# Submission with Footer(CloseIssue)
git commit -m "fix: Fixed a styling issue on the login page" -m "Closes #123"

# Use the editor to write a detailed commit message
git commit
# It will open in the editor,You can write a detailed commit message

(4) Best Practices for Submitting Information

Good commit messages:

TEXT
feat(auth): AddOAuth2.0Login Support

- SupportGoogle、GitHub、WeChat Third-Party Login
- Add Login State Persistence
- Enable Auto-RefreshTokenMechanism

Closes #456

Poor commit messages:

TEXT
update
fix bug
I made some changes.
WIP

4. Detailed Explanation of Submission Options

(1) Common Commit Options

Option Description Example
-m Submit Information git commit -m "message"
-a Automatically stage tracked files git commit -a -m "message"
--amend Modify the last commit git commit --amend
--no-verify Skip commit hook git commit --no-verify -m "message"
--allow-empty Allow empty submissions git commit --allow-empty -m "message"
-s Add Signed-off-by git commit -s -m "message"

▶ Example: Using the submission options

BASH
# Auto-check-in and commit(Applies only to tracked files)
echo "new content" >> README.md
git commit -a -m "Update README"

# Add a signature
git commit -s -m "feat: Add a New Feature"
# Will be added to the submitted information:
# Signed-off-by: Zhang San <zhangsan@example.com>

# Skippre-commitHook
git commit --no-verify -m "WIP: Save as Draft"

# View Submission Details
git show HEAD

(2) Submit Template

You can configure commit templates to ensure that team members follow a consistent format for commit messages.

BASH
# Create a commit template
cat > .git/commit-template << 'EOF'
# <type>(<scope>): <subject>
#
# typeAvailable values:
# feat, fix, docs, style, refactor, perf, test, chore
#
# subjectRules:
# - Using Imperative Sentences
# - Lowercase the first letter
# - Do not add a period at the end
EOF

# Configuring the Use of Templates
git config commit.template .git/commit-template

# Submit Using a Template
git commit
# It will open the editor,Display template content

5. Modify and Submit

(1) Modify the last commit

▶ Example: Modifying the commit message

BASH
# Edit the commit message for the last commit
git commit --amend -m "feat: Add User Login Functionality(After the correction)"

# Output:
# [main d4e5f6g] feat: Add User Login Functionality(After the correction)
#  Date: Mon Jan 1 10:00:00 2026 +0800
#  1 file changed, 1 insertion(+)

(2) Add files to the last commit

▶ Example: Adding Missing Files

BASH
# Suppose you realize after submitting that you've left out a file
git add forgotten-file.txt

# Add to the last commit,Do not modify the commit message
git commit --amend --no-edit

# Or add to and modify the commit message
git commit --amend -m "feat: Add user login functionality and related configurations"

(3) Modify the author information in the submission

▶ Example: Editing Author Information

BASH
# Change the author of the last commit
git commit --amend --author="Li Si <lisi@example.com>"

# Edit Submission Date
git commit --amend --date="2026-01-01 10:00:00"

(4) Important Notes on Modifying Submissions

100%
graph TB
    A[Commit Changes] --> B{Has the commit been pushed??}
    B -->|Not pushed| C[Can be safely modified]
    B -->|Pushed| D[Forced push required]
    D --> E[May affect other developers]
    E --> F[Use with caution]
    
    style C fill:#d4edda
    style F fill:#f8d7da

⚠️ Important Warning:


6. Submit Best Practices

(1) Atomic Commit

Each commit should contain only one logical change:

Best Practices:

BASH
# Submit1:Add Feature
git add feature.js
git commit -m "feat: Add User Login Functionality"

# Submit2:FixBug
git add fix.js
git commit -m "fix: Fix the login authentication error"

# Submit3:Update Documentation
git add README.md
git commit -m "docs: Update the Login Feature Documentation"

What Not to Do:

BASH
# A single commit containing multiple unrelated changes
git add .
git commit -m "Add Feature、FixBug、Update Documentation"

(2) Submission Frequency

(3) Pre-submission Check

▶ Example: Pre-submission Checklist

BASH
# 1. View the content to be submitted
git status

# 2. View Specific Changes
git diff

# 3. View the contents of the staging area
git diff --staged

# 4. Submit after verifying that everything is correct
git commit -m "feat: Add a New Feature"

# 5. View Submission Results
git show

(4) Ignore file configuration

Use the .gitignore file to exclude files that do not need to be submitted:

TEXT
# .gitignoreExample

# Dependency Directory
node_modules/
vendor/

# Compilation Output
dist/
build/
*.o
*.class

# IDELayout
.vscode/
.idea/
*.swp

# Environment Configuration
.env
.env.local

# Log Files
*.log
logs/

# Operating System Files
.DS_Store
Thumbs.db

❓ FAQ

Q What should I do if I realize I've left out a file after submitting?
A Use git add to add the missing files, then run git commit --amend --no-edit to append them to the last commit. Note: You can only modify commits that haven't been pushed.
Q How do I undo my last commit?
A Use git reset --soft HEAD~1 to undo a commit but keep the changes in the staging area; use git reset --mixed HEAD~1 to undo a commit and return the changes to the working directory; use git reset --hard HEAD~1 to completely discard the changes.
Q In what language should the information be submitted?
A We recommend using English, since Git and most open-source projects use English. For internal team projects, you can also use Chinese, but be sure to maintain consistency.
Q How do I view the details of a specific commit?
A Use git show <commit-id> to view details of a specific commit, including commit metadata and file differences. Use git show without any parameters to view the most recent commit.
Q Why does it say "nothing to commit" when I try to commit?
A This means the staging area is empty. You need to first use git add to stage the changes, or use git commit -a to automatically stage changes to tracked files.

📖 Summary


📝 Exercises

  1. Basic Exercise: Create a Git repository, add a README.md file, make your first commit with a proper commit message, and then view the commit history.

  2. Advanced Problem: Simulate a scenario where you discover a missing file after submitting. Use the --amend option to append the file to your last commit, and verify the commit contents.

  3. Challenge: Configure a commit template, create a template file that complies with the Conventional Commits guidelines, and use that template to make a commit, ensuring that the commit message follows the specified format.

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏