git 提交
保存 snapshots 到 history 使用 git 提交 and 编写好 提交消息.
你将学到
- 如何 创建提交使用 git 提交 -m
- 什么制作好 提交消息
- 如何 amend 最后提交
- 关系之间 staging and committing
概念讲解
git commit 的作用
git 提交 takes everything in the staging area and saves it as a permanent snapshot in the repository history. Each commit gets a unique hash (like a1b2c3d) that identifies it forever.
git commit -m "添加用户登录 page"
The -m flag lets you write the message inline. Without it, Git opens your text editor (usually Vim or Nano) for a longer message.
The Anatomy of a Commit
A commit records:
- A snapshot of all staged files at that moment
- The author name and email (from your config)
- A timestamp of when the commit was made
- The commit message describing the change
- A hash (SHA-1) that uniquely identifies this commit
- A pointer to the parent commit (forming the history chain)
Writing Good Commit Messages
A good commit message answers the question: "为什么做 I 制作这个更改?" The code itself shows what changed — the message should explain the reasoning.
Good messages:
添加 用户 登录 page 使用 OAuthFix crash 当 cart 是 空Refactor payment logic 到 separate 模块更新 README 使用 installation steps
Bad messages:
fix— what did you fix?更改— what changes?wip— what work in progress?asdf— meaningless
Conventional Commits
Many projects follow the Conventional Commits format, which prefixes messages with a type:
feat: add dark mode toggle
fix: correct total price calculation
docs: update API documentation
chore: upgrade dependencies
refactor: simplify auth flow
test: add unit tests for cart
This makes it easy to generate changelogs and automate version bumps.
Amending the Last Commit
If you made a typo in your commit message or forgot to stage a file, you can fix the last commit without making a new one:
# Fix only the message
git commit --amend -m "添加用户登录 page 使用 OAuth and 测试"
# Add a forgotten file to the previous commit
git add forgotten-file.js
git commit --amend --no-edit
Warning: never amend a commit you have already pushed to a shared remote — it rewrites history and will cause problems for other developers.
Skipping the Staging Area
If you are committing all changes to tracked files (no new files), you can skip git 添加:
git commit -am "更新登录样式"
The - flag automatically stages modified (but not new) files before committing. It does not stage untracked files.
示例代码
# Stage and commit in two steps (the standard workflow)
git add README.md
git commit -m "Add project README with setup instructions"
# Stage everything and commit with a conventional commit message
git add .
git commit -m "feat: add user authentication with JWT"
# Commit all modified tracked files in one step (skips untracked)
git commit -am "fix: correct off-by-one error in pagination"
# Multi-line commit message (opens your editor if you omit -m)
git commit -m "Refactor payment processing
- Extract validation into its own function
- Add error handling for declined cards
- Update tests to cover new flow"
# Amend the last commit (fix a typo in the message)
git commit --amend -m "feat: add user authentication with JWT tokens"
# Add a forgotten file to the last commit without changing the message
git add .env.example
git commit --amend --no-edit
Shows two-步骤添加+提交, - shortcut for tracked files, multi-行消息, amending 到 fix 消息, and amending 到添加 forgotten file. 注意 Conventional 提交样式 (feat:, fix:).
动手试一试
- Code Diff
See 什么你的提交将 包含之前 committing.
- CI/CD YAML Generator
提交触发 CI 流水线 — generate one 到 see 连接.
常见错误
错误写法
编写 vague 提交消息如 'fix' or '更改'
正确做法
提交消息应该解释 WHY 更改是 制作, 不只是什么更改 (diff shows 那个). Use Conventional 提交 (feat:, fix:, docs:) for consistency and 编写清晰, 特定消息.
错误写法
Amending 提交那个有 already 是 pushed 到 shared remote
正确做法
git 提交 --amend rewrites history. 如果你 有 already pushed 提交, amending 将 cause conflicts for anyone 谁 pulled 它. 仅 amend 局部提交那个有 不是 pushed.
相关资源
相关代码片段
- Git Log Snippet
Viewing 提交 history 之后你 有制作提交.
相关速查表
- Git Cheatsheet
git 提交 options and 消息约定.
练习 git 提交
12 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。