branch
Manage Git branches.
GitClient Methods
Section titled “GitClient Methods”Create a Branch
Section titled “Create a Branch”await git.branch(ref: string, checkout?: boolean): Promise<void>List Branches
Section titled “List Branches”await git.listBranches(): Promise<{ branches: string[]; current: string | null;}>Delete a Branch
Section titled “Delete a Branch”await git.deleteBranch(ref: string, force?: boolean): Promise<void>Low-level Functions
Section titled “Low-level Functions”import { branch, listBranchesCommand, deleteBranch, checkoutBranch,} from '@keydown-app/ts-git';branch
Section titled “branch”Create a new branch:
await branch({ fs, dir, ref, checkout?}): Promise<void>Parameters
Section titled “Parameters”| Parameter | Type | Default | Description |
|---|---|---|---|
fs | FSAdapter | required | Filesystem adapter |
dir | string | required | Working directory path |
gitdir | string | join(dir, '.git') | Git directory path |
ref | string | required | Branch name |
checkout | boolean | false | Checkout the new branch |
listBranchesCommand
Section titled “listBranchesCommand”List all branches:
await listBranchesCommand({ fs, dir}): Promise<{ branches: string[]; current: string | null }>deleteBranch
Section titled “deleteBranch”Delete a branch:
await deleteBranch({ fs, dir, ref, force?}): Promise<void>| Parameter | Type | Default | Description |
|---|---|---|---|
ref | string | required | Branch name to delete |
force | boolean | false | Force delete even if not merged |
Example
Section titled “Example”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 initial commitawait fs.writeFile('/my-repo/README.md', '# Project', 'utf8');await git.add('README.md');await git.commit('Initial commit', { name: 'John Doe', email: 'john@example.com',});
// List branches (just 'main' for now)const list1 = await git.listBranches();console.log(list1.branches); // ['main']console.log(list1.current); // 'main'
// Create a new branch and checkoutawait git.branch('feature', true);
// List againconst list2 = await git.listBranches();console.log(list2.branches); // ['main', 'feature']console.log(list2.current); // 'feature'
// Switch back to mainawait git.checkoutBranch('main');
// Delete the feature branchawait git.deleteBranch('feature');
const list3 = await git.listBranches();console.log(list3.branches); // ['main']