git 添加
Staging 更改使用 git 添加 — 如何 prepare files for 下一个提交.
你将学到
- 如何 stage individual files, 多个 files, or 一切
- difference 之间 git 添加 . and git 添加 -
- 如何 stage 部分的 file 使用 git 添加 -p
- 什么 happens 当你 stage file 那个是 already staged
概念讲解
git add 的作用
git 添加 moves changes from your working directory into the staging area. It tells Git: "I 想要到包含这些更改 in my 下一个提交." Nothing is saved to history yet — that happens with git 提交.
git add README.md
Staging Options
# Stage a single file
git add README.md
# Stage multiple specific files
git add index.html style.css script.js
# Stage all changes in the current directory and below
git add .
# Stage all changes in the entire repository (including deletions)
git add -A
# or equivalently:
git add --all
# Stage all changes in the current directory (including deletions here)
git add -u
git add . vs git add -A
This is a subtle but important distinction:
git 添加 .— stages new and modified files in the current directory and its subdirectories. It does NOT stage deletions in older Git versions (pre-2.0). In modern Git (2.0+), it stages everything in the current directory tree.git 添加 -— stages everything in the entire repository, including deletions, regardless of which directory you are in.
Best practice: when starting out, use git add -A or git add . from the repository root. As you advance, stage files individually to keep commits focused.
Staging Parts of a File
Sometimes you have multiple unrelated changes in the same file and want to commit them separately. git 添加 -p (patch mode) lets you stage changes hunk by hunk:
git add -p script.js
Git will show you each change and ask: y (yes, stage this hunk), n (no), s (split into smaller hunks), q (quit). This is powerful for creating clean, focused commits.
Re-staging a Modified File
If you stage a file, then modify it again, the new changes are not staged. The staging area holds a snapshot from when you ran git 添加. You need to git 添加 the file again to stage the latest changes:
git add README.md # stage version 1
echo "更多文本" >> README.md # now version 2
git status # shows README as both staged AND modified
git add README.md # re-stage version 2
What git add Does NOT Do
- It does not save anything to history — that is
git 提交. - It does not push to a remote — that is
git 推送. - It does not permanently lock the changes — you can unstage with
git reset.
示例代码
# Create some files to work with
echo "# My Project" > README.md
echo "body { margin: 0; }" > style.css
echo "console.log('hi');" > script.js
# Stage a single file
git add README.md
git status # README.md is staged, the others are untracked
# Stage the rest
git add style.css script.js
git status # all three are now staged
# Modify a file after staging
echo "## Installation" >> README.md
git status # README.md is now both staged AND modified
# Re-stage the updated file
git add README.md
git status # README.md is fully staged again
# Stage everything at once (common workflow)
git add .
git status
# Interactive staging — stage parts of a file
git add -p script.js
Shows staging individual files, staging 多个 files, re-staging 需求之后 modification, staging 一切, and interactive patch 模式. Use git 状态之后每个 command 到 see 什么是 staged.
动手试一试
- Code Diff
Compare two versions — Git shows diffs 当你 stage and 提交.
- .gitignore Generator
Keep files out 的 staging 使用 proper .gitignore.
常见错误
错误写法
Staging file, 编辑它, and forgetting 到 re-stage 之前 committing
正确做法
git 添加 takes snapshot at 时刻你 运行它. 如果你 编辑 file afterward, 新更改是 不 staged. 运行 git 添加再次之前 committing, or 检查 git 状态到 see 如果 file 是两者都 staged and 修改.
错误写法
Using git 添加 . and accidentally staging files 那个应该是 ignored
正确做法
创建 .gitignore file 首先到 排除构建 artifacts, node_modules, .环境 files, etc. 然后 git 添加 . 将 skip 它们 automatically.
相关资源
相关速查表
- Git Cheatsheet
git 添加 options and staging 模式.
练习 git 添加
9 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。