Function Signature
function add(
args: "." | readonly string[] = ".",
opts: AddOptions = {}
): Promise<string>
Add file contents to the staging area (index) in preparation for a commit.
Parameters
args
'.' | readonly string[]
default:"'.'"
Files to add to the staging area. Can be:
"." - Add all files in the current directory and subdirectories
- Array of file paths - Add specific files or patterns
Configuration options for the add operation.Show AddOptions properties
Additional flags to pass to git add. Supported flags:
"--dry-run" - Don’t actually add files, just show what would be added
"--force" - Allow adding otherwise ignored files
"--sparse" - Allow updating index entries outside the sparse-checkout cone
"--update" - Update tracked files (don’t add new files)
"--all" - Add, modify, and remove index entries to match the working tree
"--ignore-removal" - Only add new/modified files, ignore removed files
"--intent-to-add" - Record only the fact that the path will be added later
"--refresh" - Don’t add files, just refresh their stat() info in the index
"--ignore-errors" - Continue if errors occur
"--ignore-missing" - Check if any files would be ignored and exit with error status
"--renormalize" - Renormalize EOL of tracked files
"--chmod=+x" - Add executable permission to files
"--chmod=-x" - Remove executable permission from files
Returns
A promise that resolves to the output from the git add command.
Examples
Add all files
import { add } from "usegit"
// Add all files in the current directory
await add()
// Explicitly add all files
await add(".")
Add specific files
// Add a single file
await add(["src/index.ts"])
// Add multiple files
await add(["src/index.ts", "package.json", "README.md"])
// Add files with pattern
await add(["src/**/*.ts"])
Add with options
// Dry run - see what would be added
await add(".", { flags: ["--dry-run"] })
// Force add ignored files
await add(["dist/output.js"], { flags: ["--force"] })
// Update only tracked files
await add(".", { flags: ["--update"] })
// Add with executable permission
await add(["scripts/build.sh"], { flags: ["--chmod=+x"] })
// Multiple flags
await add(["src/"], {
flags: ["--update", "--ignore-errors"]
})
Intent to add
// Mark new files as tracked without staging content
// Useful for showing them in git diff
await add(["new-file.ts"], { flags: ["--intent-to-add"] })
- commit - Create a commit with staged changes
- commitAll - Commit all tracked changes without explicit staging