Skip to content

add

Add a file to the git index (staging area).

await git.add(filepath: string): Promise<void>
await git.addAll(): Promise<void>
ParameterTypeDescription
filepathstringPath to the file to stage
// Stage a single file
await git.add('README.md');
// Stage all changes
await git.addAll();
import { add, addAll, remove } from '@keydown-app/ts-git';
await add({
fs,
dir,
filepath
}): Promise<void>
ParameterTypeDefaultDescription
fsFSAdapterrequiredFilesystem adapter
dirstringrequiredWorking directory path
gitdirstringjoin(dir, '.git')Git directory path
filepathstringrequiredFile path to stage

Stages all modified and untracked files:

await addAll({
fs,
dir
}): Promise<void>
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 files
await fs.writeFile('/my-repo/README.md', '# Hello', 'utf8');
await fs.writeFile('/my-repo/src/index.ts', 'console.log("hi");', 'utf8');
// Stage files
await git.add('README.md'); // Stage single file
await git.add('src/index.ts'); // Stage another file
// Or stage everything at once
// await git.addAll();
// Commit
await git.commit('Add initial files', {
name: 'John Doe',
email: 'john@example.com',
});