Linux command
git-add 命令
安全
权限或系统影响较大,执行前请核对目标。
常用示例
Stage a specific file
git add [path/to/file]
Stage all changes
git add .
Stage all changes
git add -A
Stage only modified and deleted
git add -u
Interactively select hunks
git add -p [path/to/file]
Stage files matching a pattern
git add "*.js"
Force add ignored files
git add -f [path/to/ignored/file]
Interactive mode
git add -i
说明
git add stages changes in the working directory for the next commit. It adds content to the staging area (index), creating a snapshot of files that will be included when git commit is run. Changes can be staged at the file level or selectively at the hunk level using -p (patch mode). In patch mode, you can review each change and choose whether to stage it, allowing fine-grained control over commits. The staging area allows you to prepare commits incrementally, choosing exactly which changes to include. Files can be unstaged using git restore --staged or git reset.
参数
- -A, --all
- Stage all changes (new, modified, deleted) in entire working tree.
- -u, --update
- Stage modifications and deletions of tracked files only, not new files.
- -p, --patch
- Interactively choose hunks to stage from each file.
- -i, --interactive
- Enter interactive mode for selecting files to stage.
- -f, --force
- Allow adding ignored files.
- -n, --dry-run
- Show what would be staged without actually staging.
- -v, --verbose
- Show files as they are added.
- --intent-to-add
- Record only that the path will be added later (stage empty content).
- --refresh
- Refresh the index without adding files.
FAQ
What is the git-add command used for?
git add stages changes in the working directory for the next commit. It adds content to the staging area (index), creating a snapshot of files that will be included when git commit is run. Changes can be staged at the file level or selectively at the hunk level using -p (patch mode). In patch mode, you can review each change and choose whether to stage it, allowing fine-grained control over commits. The staging area allows you to prepare commits incrementally, choosing exactly which changes to include. Files can be unstaged using git restore --staged or git reset.
How do I run a basic git-add example?
Run `git add [path/to/file]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -A, --all do in git-add?
Stage all changes (new, modified, deleted) in entire working tree.