add
Add a file to the git index (staging area).
GitClient Methods
Section titled “GitClient Methods”Add a Single File
Section titled “Add a Single File”await git.add(filepath: string): Promise<void>Add All Changes
Section titled “Add All Changes”await git.addAll(): Promise<void>Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
filepath | string | Path to the file to stage |
Example
Section titled “Example”// Stage a single fileawait git.add('README.md');
// Stage all changesawait git.addAll();Low-level Functions
Section titled “Low-level Functions”import { add, addAll, remove } from '@keydown-app/ts-git';await add({ fs, dir, filepath}): Promise<void>| Parameter | Type | Default | Description |
|---|---|---|---|
fs | FSAdapter | required | Filesystem adapter |
dir | string | required | Working directory path |
gitdir | string | join(dir, '.git') | Git directory path |
filepath | string | required | File path to stage |
addAll
Section titled “addAll”Stages all modified and untracked files:
await addAll({ fs, dir}): Promise<void>Complete Example
Section titled “Complete Example”import { GitClient, MemoryFSAdapter } from '@keydown-app/ts-git';
const fs = new MemoryFSAdapter();const git = new GitClient({ fs, dir: '/my-repo' });
await git.init();
// Create some filesawait fs.writeFile('/my-repo/README.md', '# Hello', 'utf8');await fs.writeFile('/my-repo/src/index.ts', 'console.log("hi");', 'utf8');
// Stage filesawait git.add('README.md'); // Stage single fileawait git.add('src/index.ts'); // Stage another file
// Or stage everything at once// await git.addAll();
// Commitawait git.commit('Add initial files', { name: 'John Doe', email: 'john@example.com',});