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:
- Snapshot: The complete state of all tracked files at a given point in time
- Metadata: Author information, submission date, submission details
- Parent commit: A pointer to the previous commit (except for the first commit)
- Unique Identifier: 40-character SHA-1 hash value
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:
- Modify File: Edit a file in the workspace
- Stage Changes: Use
git addto add changes to the staging area - Create a commit: Use
git committo commit the contents of the staging area to the repository
(3) The Purpose of Submission
- Version History: Saves the complete state of the project at a specific point in time
- Version History: You can view and revert to any version at any time
- Collaboration Basics: Team members can collaborate based on commits
- Change Notes: Describe the details of this modification in the submission notes.
2. Basic Submission Operations
(1) First Submission
▶ Example: Creating Your First Commit
# 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
# 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
# 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:
- Clearly explain changes: Help team members quickly understand what has been modified
- Easy to Trace History: Makes it easy to find development records for specific features
- Automation Tool Support: Supports automatic generation of change logs
- Improve collaboration efficiency: Reduce communication costs
(2) Conventional Commits Standard
This is currently the most popular commit message convention, and the format is as follows:
<type>(<scope>): <subject>
<body>
<footer>
Explanation of Each Section:
- type (required): Submission type
- scope (optional): Scope of impact
- subject (required): Brief description
- body (optional): Detailed description
- footer (optional): Footer information
(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
# 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:
feat(auth): AddOAuth2.0Login Support
- SupportGoogle、GitHub、WeChat Third-Party Login
- Add Login State Persistence
- Enable Auto-RefreshTokenMechanism
Closes #456
Poor commit messages:
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
# 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.
# 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
# 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
# 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
# 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
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:
- Modify only commits that haven't been pushed
- Modifications to a pushed commit require
git push --force - Forced pushes can affect other developers, so communication in advance is necessary.
- When working as a team, try to avoid modifying commits that have already been pushed
6. Submit Best Practices
(1) Atomic Commit
Each commit should contain only one logical change:
Best Practices:
# 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:
# A single commit containing multiple unrelated changes
git add .
git commit -m "Add Feature、FixBug、Update Documentation"
(2) Submission Frequency
- Frequent commits: Take small, rapid steps; commit after completing each small feature
- Complete commit: Each commit should represent a complete state.
- Avoid half-finished work: Do not submit code that does not work
(3) Pre-submission Check
▶ Example: Pre-submission Checklist
# 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:
# .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
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.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.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.git add to stage the changes, or use git commit -a to automatically stage changes to tracked files.📖 Summary
- Committing is a core Git operation that creates a snapshot of a version and records the project's history
- Commit Process: Modify files → Stage changes → Create a commit
- Write clear commit messages following the Conventional Commits guidelines
- Use commit options to improve efficiency: -a to automatically stage changes, --amend to modify a commit
- Modifying a commit that has already been pushed requires a force push, which may affect other developers.
- Best Practices for Committing: Atomic commits, frequent commits, and pre-commit checks
📝 Exercises
-
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.
-
Advanced Problem: Simulate a scenario where you discover a missing file after submitting. Use the
--amendoption to append the file to your last commit, and verify the commit contents. -
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.



