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';Create a Filesystem
Section titled “Create a Filesystem”TS-Git uses a pluggable filesystem adapter. For this example, we’ll use the in-memory adapter:
const fs = new MemoryFSAdapter();Initialize the Client
Section titled “Initialize the Client”Create a GitClient instance with your filesystem and directory:
const git = new GitClient({ fs, dir: '/my-repo', defaultBranch: 'main',});Initialize a Repository
Section titled “Initialize a Repository”Create a new Git repository:
await git.init();console.log('Repository initialized!');Add Files
Section titled “Add Files”Create and stage some files:
// Write a file to the filesystemawait fs.writeFile( '/my-repo/README.md', '# My Project\n\nHello World!', 'utf8',);
// Stage the fileawait git.add('README.md');console.log('File staged!');Commit Changes
Section titled “Commit Changes”Create your first commit:
const commitHash = await git.commit('Initial commit', { name: 'John Doe', email: 'john@example.com',});
console.log('Created commit:', commitHash);Check Status
Section titled “Check Status”View the working tree status:
const status = await git.status('README.md');console.log('File status:', status);View History
Section titled “View History”See your commit history:
const commits = await git.log();console.log('Commits:', commits);Complete Example
Section titled “Complete Example”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();Next Steps
Section titled “Next Steps”- Commands - Learn about all available commands
- Filesystem Adapters - Explore different storage options