Skip to content

commit

Create a new commit with the staged changes.

await git.commit(
message: string,
author: Author
): Promise<string>
ParameterTypeDescription
messagestringCommit message
authorAuthorCommit author information
interface Author {
name: string;
email: string;
}

Returns the SHA-1 hash of the created commit.

import { commit } from '@keydown-app/ts-git';
await commit({
fs,
dir,
message,
author
}): Promise<string>
ParameterTypeDefaultDescription
fsFSAdapterrequiredFilesystem adapter
dirstringrequiredWorking directory path
gitdirstringjoin(dir, '.git')Git directory path
messagestringrequiredCommit message
authorAuthorrequiredCommit author { name, email }
import { GitClient, MemoryFSAdapter } from '@keydown-app/ts-git';
const fs = new MemoryFSAdapter();
const git = new GitClient({ fs, dir: '/my-repo', defaultBranch: 'main' });
await git.init();
// Create and stage a file
await fs.writeFile('/my-repo/README.md', '# My Project', 'utf8');
await git.add('README.md');
// Create commit
const commitHash = await git.commit('Initial commit', {
name: 'John Doe',
email: 'john@example.com',
});
console.log('Created commit:', commitHash);
// Output: Created commit: a1b2c3d4e5f6...
// First commit
await fs.writeFile('/my-repo/README.md', '# My Project', 'utf8');
await git.add('README.md');
await git.commit('Initial commit', {
name: 'John Doe',
email: 'john@example.com',
});
// Second commit
await fs.writeFile('/my-repo/src/index.ts', 'console.log("hello");', 'utf8');
await git.add('src/index.ts');
await git.commit('Add index.ts', {
name: 'John Doe',
email: 'john@example.com',
});
// View history
const commits = await git.log();
console.log(commits.length); // 2
  • add - Stage files
  • log - View commit history