Skip to main content

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
opts
AddOptions
default:"{}"
Configuration options for the add operation.

Returns

result
Promise<string>
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

Build docs developers (and LLMs) love