Skip to content

branch

Manage Git branches.

await git.branch(ref: string, checkout?: boolean): Promise<void>
await git.listBranches(): Promise<{
branches: string[];
current: string | null;
}>
await git.deleteBranch(ref: string, force?: boolean): Promise<void>
import {
branch,
listBranchesCommand,
deleteBranch,
checkoutBranch,
} from '@keydown-app/ts-git';

Create a new branch:

await branch({
fs,
dir,
ref,
checkout?
}): Promise<void>
ParameterTypeDefaultDescription
fsFSAdapterrequiredFilesystem adapter
dirstringrequiredWorking directory path
gitdirstringjoin(dir, '.git')Git directory path
refstringrequiredBranch name
checkoutbooleanfalseCheckout the new branch

List all branches:

await listBranchesCommand({
fs,
dir
}): Promise<{ branches: string[]; current: string | null }>

Delete a branch:

await deleteBranch({
fs,
dir,
ref,
force?
}): Promise<void>
ParameterTypeDefaultDescription
refstringrequiredBranch name to delete
forcebooleanfalseForce delete even if not merged
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 commit
await 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 checkout
await git.branch('feature', true);
// List again
const list2 = await git.listBranches();
console.log(list2.branches); // ['main', 'feature']
console.log(list2.current); // 'feature'
// Switch back to main
await git.checkoutBranch('main');
// Delete the feature branch
await git.deleteBranch('feature');
const list3 = await git.listBranches();
console.log(list3.branches); // ['main']