mirror of
https://github.com/mintlify/docs.git
synced 2026-09-14 13:35:46 +08:00
153 lines
2.3 KiB
Plaintext
153 lines
2.3 KiB
Plaintext
---
|
|
title: git add
|
|
description: Add file contents to the staging area
|
|
---
|
|
|
|
Stage changes for the next commit.
|
|
|
|
## Syntax
|
|
|
|
```bash
|
|
git add [options] <pathspec>
|
|
```
|
|
|
|
## Description
|
|
|
|
`git add` moves changes from your working directory to the staging area. Staged changes are included in the next commit. This lets you carefully select which changes to commit.
|
|
|
|
## Common usage
|
|
|
|
Stage a specific file:
|
|
|
|
```bash
|
|
git add filename.txt
|
|
```
|
|
|
|
Stage all changes in current directory:
|
|
|
|
```bash
|
|
git add .
|
|
```
|
|
|
|
Stage all changes in repository:
|
|
|
|
```bash
|
|
git add --all
|
|
```
|
|
|
|
## Options
|
|
|
|
<ParamField path="-A, --all" type="flag">
|
|
Stage all changes (new, modified, and deleted files)
|
|
|
|
```bash
|
|
git add --all
|
|
```
|
|
</ParamField>
|
|
|
|
<ParamField path="-u, --update" type="flag">
|
|
Stage modified and deleted files, but not new files
|
|
</ParamField>
|
|
|
|
<ParamField path="-p, --patch" type="flag">
|
|
Interactively choose which changes to stage
|
|
|
|
```bash
|
|
git add --patch
|
|
```
|
|
</ParamField>
|
|
|
|
<ParamField path="-n, --dry-run" type="flag">
|
|
Show what would be staged without actually staging
|
|
</ParamField>
|
|
|
|
## Examples
|
|
|
|
### Stage a single file
|
|
|
|
```bash
|
|
git add README.md
|
|
```
|
|
|
|
### Stage multiple files
|
|
|
|
```bash
|
|
git add file1.txt file2.txt file3.txt
|
|
```
|
|
|
|
### Stage all files in a directory
|
|
|
|
```bash
|
|
git add src/
|
|
```
|
|
|
|
### Stage all changes
|
|
|
|
```bash
|
|
git add .
|
|
```
|
|
|
|
or
|
|
|
|
```bash
|
|
git add --all
|
|
```
|
|
|
|
### Stage only tracked files
|
|
|
|
```bash
|
|
git add -u
|
|
```
|
|
|
|
### Interactive staging
|
|
|
|
```bash
|
|
git add -p
|
|
```
|
|
|
|
This lets you review each change and choose whether to stage it.
|
|
|
|
## Best practices
|
|
|
|
### Review before staging
|
|
|
|
```bash
|
|
git diff # See unstaged changes
|
|
git add specific-file.txt # Stage selectively
|
|
git diff --staged # Review staged changes
|
|
```
|
|
|
|
### Stage related changes together
|
|
|
|
Group related changes in the same commit:
|
|
|
|
```bash
|
|
git add api/endpoints.js api/routes.js
|
|
git commit -m "Add new API endpoints"
|
|
```
|
|
|
|
### Use .gitignore
|
|
|
|
Create a `.gitignore` file to prevent staging unwanted files:
|
|
|
|
```gitignore
|
|
node_modules/
|
|
.env
|
|
*.log
|
|
```
|
|
|
|
## Unstaging files
|
|
|
|
If you staged something by mistake:
|
|
|
|
```bash
|
|
git restore --staged filename.txt
|
|
```
|
|
|
|
## Related commands
|
|
|
|
- [git status](/git/status) - Check what's staged
|
|
- [git commit](/git/commit) - Save staged changes
|
|
- [git diff](/git/diff) - Review changes
|
|
- [git restore](/git/restore) - Unstage or discard changes
|