Skip to content

Quick Start

This guide will walk you through creating your first Git repository with TS-Git.

First, import the necessary classes:

import { GitClient, MemoryFSAdapter } from '@keydown-app/ts-git';

TS-Git uses a pluggable filesystem adapter. For this example, we’ll use the in-memory adapter:

const fs = new MemoryFSAdapter();

Create a GitClient instance with your filesystem and directory:

const git = new GitClient({
fs,
dir: '/my-repo',
defaultBranch: 'main',
});

Create a new Git repository:

await git.init();
console.log('Repository initialized!');

Create and stage some files:

// Write a file to the filesystem
await fs.writeFile(
'/my-repo/README.md',
'# My Project\n\nHello World!',
'utf8',
);
// Stage the file
await git.add('README.md');
console.log('File staged!');

Create your first commit:

const commitHash = await git.commit('Initial commit', {
name: 'John Doe',
email: 'john@example.com',
});
console.log('Created commit:', commitHash);

View the working tree status:

const status = await git.status('README.md');
console.log('File status:', status);

See your commit history:

const commits = await git.log();
console.log('Commits:', commits);

Here’s the complete code:

import { GitClient, MemoryFSAdapter } from '@keydown-app/ts-git';
async function main() {
// Setup
const fs = new MemoryFSAdapter();
const git = new GitClient({ fs, dir: '/my-repo', defaultBranch: 'main' });
// Initialize
await git.init();
// Create and stage a file
await fs.writeFile('/my-repo/README.md', '# My Project', 'utf8');
await git.add('README.md');
// Commit
const hash = await git.commit('Initial commit', {
name: 'John Doe',
email: 'john@example.com',
});
console.log('Created commit:', hash);
// View history
const commits = await git.log();
console.log('History:', commits);
}
main();